Moving Character to mouse click with Rigidbody2D!
Hello guys ..
I'm building my first game ever ^_^ .. just a simple 2D game .. Now i'm implementing the character movement .. The idea is (when i click somewhere in the Screen or the scene , the character goes to that click position .. and if there is no new target .. the player falls down ) .. i tried to move the player using Rigidbody and AddForce method .. but it didn't work .. because the character keeps accelerating and there are a lot of weird stuff going on with that way .. So now i'm moving the character using transform.position ., and it's work .. this the code ..
public class Player : MonoBehaviour
{
[SerializeField]
private float _speed;
private bool _move;
private Vector3 _target;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
_target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
_target.z = transform.position.z;
if (_move == false)
{
_move = true;
}
}
if (_move == true)
{
transform.position = Vector3.MoveTowards(transform.position, _target, _speed * Time.deltaTime);
}
}
}
But in this way .. the character reach the target but doesn't fall .. because there is no Rigidbody2D ..
How can i add rigidbody2D to make the character fall when reach target .. without ruining the whole movement ??
Your answer
Follow this Question
Related Questions
Constraning an object's forces to only two directions in 2D (or infinite friction?) 0 Answers
Unity2D Help with rigidbody character movement. 0 Answers
How do I assign a velocity to an object on only 1 axis? 1 Answer
Why/How 2d tower of blocks collapse? 0 Answers
2D Top-Down Character: How to make my character move using physics? 1 Answer