Move rigid body to mouse position
Hello, I am trying to make my player move to wherever the user clicked but I am getting strange results.
I am trying to get the world position from where the user clicked, calculate a direction from that and add a force to the rigid body. But right now it just makes the player do a strange circular motion...
private void FixedUpdate()
{
if (movementMode == MovementMode.Touch)
{
Vector3 worldClickPosition = Vector3.zero;
if (Input.mousePresent == true && Input.GetMouseButton(0) == true)
{
worldClickPosition = sceneCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, sceneCamera.nearClipPlane));
Vector3 direction = worldClickPosition - playerRigidBody.position;
direction.y = 0f;
playerRigidBody.AddForce(direction * forceMultiplier);
}
}
else
{
HandleNonTouchInput();
}
}
Comment