the movement of my object is not smooth - c#

I'm making a mobile game based on android. When I was making a transfer code for my player object, I wrote code like this.
void Update() {
if(Input.touchcount == 1)
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
//Move
}
else if(touch.phase == TouchPhase.Stationary)
{
//Move
}
}
}
It's moving at right direction and speed that I wanted. But when I touch quicker, these codes are don't working smoothly. I mean the player object is not moving smoothly.
Is there any solution for this problem?
P.S. I cant use AddForce in rigidbody

Related

How can I make my Raycast2D touch postion more precise?

I have a problem in my mobile game , that I'm currently developing in Unity.
In my script I detect touches via a RaycastHit2D and then destroy the touched object (In my case a raindrop).
The only problem is that when I run the game and touch the raindrop, it doesn't always work.
It only works , when I slightly touch below it.
The Circle Collider 2D I'm using for my raindrop prefab is alright , so I don't think it causes the trouble.
My Script:
void Update()
{
GetInput();
}
private void GetInput()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Vector2 WorldPoint = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit = Physics2D.Raycast(WorldPoint, Vector2.zero);
GameObject collider = hit.collider.gameObject;
if (collider.tag == "Tropfen")
{
Destroy(collider);
}
}
}
}

How to AddFroce to a Rigidbody 2D on 1 screen-touch?

I'm working on a simple android game in Unity 2D, I have to make our player jump by adding force or changing the velocity of its Rigidbody2D (I know how). The problem is I don't know how to tell unity that add force on single screen tap (I'm not familiar with touch inputs) so any help would be appreciated. Please keep it clean as I'm just a beginner.
Here's the script I made so far.
{
public Rigidbody2D _playerRB;
public bool _canMove;
public float _speed; //keep this above cam speed (relative)
public float _jumpForce;
void Start()
{
_playerRB = GetComponent<Rigidbody2D>();
}
void Update()
{
_playerRB.velocity = new Vector2(_speed, _playerRB.velocity.y);
if (Input.touchCount > 0)
{
_playerRB.velocity = new Vector2(_playerRB.velocity.x, _jumpForce);
}
}
}
You can achieve this by handling the touch input in your Update function:
void Update()
{
_playerRB.velocity = new Vector2(_speed, _playerRB.velocity.y);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
_playerRB.velocity = new Vector2(_playerRB.velocity.x, _jumpForce);
}
}
}
Also there are different TouchPhases such as TouchPhase.Moved which indicates that the touch input has moved and TouchPhase.Ended which indicates that the finger has stopped touching the screen. Using these in your Update you can achieve a lot with touch controls.

Can't solve hitbox issues in mobile game made in Unity

I am developing a game where objects fall from the top of the screen, and you try to and tap on them before they reach the bottom of the screen. The code works fine when I played the game in the editor (with the touch controls swapped to mouse controls), except when I run the game on a phone, the game only seems to register a successful hit if you tap slightly in front of the object in the direction that it is traveling, and does not register a hit if you tap towards the back end or center of the object. I have built and ran the game over 10 times now, each time trying to fix this issue but nothing seems to help. My theory at the moment is that my code for the touch controls have too much going on and/ or have redundancies and by the time it checks whether or not an object is at the position of the touch, the object has moved to a different location. Any thoughts on why the hit boxes are off, and is there a better way to do hit detection with touch screen?
void FixedUpdate()
{
if (IsTouch())
{
CheckTouch(GetTouchPosition());
}
}
// Returns true if the screen is touched
public static bool IsTouch()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
return true;
}
}
return false;
}
// Gets the position the touch
private Vector2 GetTouchPosition()
{
Vector2 touchPos = new Vector2(0f, 0f);
Touch touch = Input.GetTouch(0);
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
touchPos = touch.position;
}
return touchPos;
}
// Checks the position of the touch and destroys the ball if the
ball is touched
private void CheckTouch(Vector2 touchPos)
{
RaycastHit2D hit =
Physics2D.Raycast(Camera.main.ScreenToWorldPoint(
(Input.GetTouch(0).position)), Vector2.zero);
if (hit.collider != null)
{
destroy(hit.collider.gameObject);
}
}

Raycast not hitting objects

It's like a mario game. The player is jumping around and has to collect some items.
The problem is that my ray isn't colliding with the item box colliders.
I need the ray to know, so i can destroy the right item that the player has collided.
void OnCollisionEnter2D(Collision2D colisor)
{
if((colisor.gameObject.name == "floor" || colisor.gameObject.name == "floor2" || colisor.gameObject.name == "floor3"))
{
anim.SetBool("jump", false);
anim.SetFloat("speed", 0);
}
if (colisor.gameObject.name == "space(Clone)")
{
RaycastHit hit;
Ray ray = new Ray(player.position, transform.right);
Debug.Log("hit1");
if (Physics.Raycast(ray, out hit))
{
BoxCollider bc2d = hit.collider as BoxCollider;
Debug.Log("hit2");
if (bc2d != null)
{
Destroy(bc2d.gameObject);
}
}
}
}
You're mixing 3d and 2d physics; Physics will only look for 3d objects, so you should be using Physics2D instead. This raycast may still fail if the cast starts inside the target, because the surface normals point in the wrong direction.
Also note that since you already have the Collision2D, you can just grab the otherCollider and shouldn't need to raycast in the first place.
Physics.Raycast isn't working with 2D objects. Instead you need to use Physics2D.Raycast or Graphic Raycaster.
Raycast2D - https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Graphic Raycaster - https://docs.unity3d.com/eng/current/Manual/script-GraphicRaycaster.html

Raycast not working in Update() Unity c#

I am making a game where an object follows my finger. But I do not want the object to jump to my finger if I tap anywhere on the screen. I am using a raycast to do this. It works perfectly except if I move my finger too fast the object freezes.
My theory is that because of it being in Update() that once I move my finger off the object it detects that I can no longer drag it. I have no idea how to fix this.
public GameObject Bigball;
public GameObject Smallball;
// Update is called once per frame
private void Update ()
{
// lets ball follow finger
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary)
{
var touch = Input.GetTouch(0);
Vector3 fingerPos = touch.position;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(fingerPos);
if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "ball"))
{
Smallball.SetActive(false);
Bigball.SetActive(true);
Vector3 pos = new Vector3((Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)).x,
(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)).y, 0);
Bigball.transform.position = pos;
}
}
else
{
Bigball.SetActive(false);
Smallball.SetActive(true);
Smallball.transform.position = new Vector3(Bigball.transform.position.x, Bigball.transform.position.y, 0);
}
}
It might make more sense to only Raycast once when the finger touches the screen. If that Raycast finds an object to drag, simply store that object and move it to follow the finger every Update(), as long as the finger continues to touch the screen.
Once the finger is released, just forget about the object until the next touch finds a new object to drag.
An alternative would be to use no Raycasts at all but build on IBeginDragHandler, IDragHandler and IEndDragHandler, as Bijan already mentioned.

Categories

Resources