- Home /
How to turn ship smoothly to mouse position when mouse click?
Hi, i'm relatively new to Unity and coding. I'm trying to make a simple 2D ship movement script based on mouse click. But i want it to turn realistically (slowly) like a ship in space to the mouse click instead of instantly. can someone help?
public class MouseMovement : MonoBehaviour {
public float speed = 1;
private Vector3 target;
private Vector3 direction;
void Start () {
target = transform.position;
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
direction = target - transform.position;
transform.rotation = Quaternion.LookRotation (Vector3.forward, direction);
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
Smooth ship rotation 1 Answer
Rotate an object such that it is theta degrees relative to another object 1 Answer
Random insect movement 1 Answer
Look Where You're Going 3 Answers