- Home /
Ship rotation while moving forward
I am making ship move to mouse click. The ship goes to where i clicked, and the ship even rotates before it moves towards the click. The only problem i see is that when the ship moves forward, moves from speed 0 to the speed i set it too. I have tried using Lerp to accelerate smoothly before moving and slow down before stopping. The ship however takes the same amount of time to get from point A to point B regardless of distance, so things look funny.
If someone could help me out with this, that would be awesome. I need to find a way to add acceleration to the ship. And for a plus, it would be amazing if you guys could tell me how to make the ship move forward so the ship makes a small semi circle before heading towards the direction i clicked.
Here is the code i have so far.
public float speed; private Vector3 endPoint; private Quaternion rotation; float tempTime; bool start_time = false; float half_way; void Update() { float step = speed * Time.deltaTime; // if left mouse clicked if (Input.GetMouseButton (0)) { endPoint = Input.mousePosition; endPoint.z = 20.0f; // position of main camera on the z axis //get endpoint relative to the camera endPoint = Camera.main.ScreenToWorldPoint(endPoint); // set rotation value rotation = Quaternion.LookRotation (endPoint - transform.position); tempTime = 0.0f; start_time = true; half_way = (endPoint - transform.position).magnitude / 2; } transform.rotation = Quaternion.Slerp(transform.rotation, rotation, (Time.deltaTime * speed / 4)); if (start_time) { tempTime += Time.deltaTime / 8; } // if rotation is half way done. if (tempTime > step){ //get direction Vector3 dir = endPoint - transform.position; // distance from A to B float distance = dir.magnitude; if (distance > step) { transform.position += dir.normalized * step; } else { transform.position = endPoint; transform.rotation = rotation; start_time = false; } } }
Thank you in advance.
Your answer
Follow this Question
Related Questions
C# move a cube slowly 1 Answer
Spaceship movement with acceleration and deceleration on Unity 0 Answers
Scrolling Sprite Object 0 Answers
Moving child and parent 1 Answer