Rotating, moving and keep on going with constant velocity in direction of click
Hi, I am trying to create a 2D object which rotates and move in the direction of mouse click/touch. Also, I want this object to keep on going with constant speed in same direction even if it reaches the click/touch position. It should only change direction when user again clicks/touches the screen. I really appreciate all the help, thank you.
i have used following code to move the object from point A to B and this works fines but objects stops after reaching point B. I want this object to keep moving in same direction.
Answer by atrip · Jan 18, 2018 at 05:04 AM
public Vector2 targetPosition;
public float speed = 5f;
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//print (targetPosition);
}
transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Rotate player 0 Answers
Rotation and movement of a rolling sphere 0 Answers
Rotate the velocity of player 0 Answers
Mouse controlled 2D object bleeding through walls 1 Answer
how to lean an object smothly towards its moving direction by its velocity? 1 Answer