- Home /
Ship rotate to look at over time
So Robertbu has been super helpful with getting my basic game play setup. Basically I want to rotate the ship to 'look at' the new position it will be moving to over time. I do not want it to stop look in new direction then continue to move. I just want it to turn slowly into the direction while its moving. However when it's stationary, I don't want it to move until its fully looking in that direct.
Something on the lines like this https://www.youtube.com/watch?v=hAVXSBnsEtA
Demo: http://cerberium.com/test/Test.html
#pragma strict
var speed = 35.0; // Degrees per second
var delay = 2.0;
private var direction = Vector3.zero;
private var qTo = Quaternion.identity;
private var timeStamp : float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var mousePos = Input.mousePosition;
mousePos.z = 1000; // select distance = 10 units from the camera
var worldPos = Camera.main.ScreenToWorldPoint(mousePos);
direction = (worldPos - transform.position).normalized;
timeStamp = Time.time + delay;
qTo = Quaternion.LookRotation(direction);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
if (direction != Vector3.zero && timeStamp < Time.time)
transform.position += direction * Time.deltaTime;
}
Answer by 2d4Games · Aug 14, 2013 at 07:57 PM
At line 18 you're setting the timeStamp to be 2 seconds in the future, and at line 25 you're making the if statement not get triggered until those 2 seconds pass.
From what I see, after you click, your ship would stop having its position transformed, and then start again after 2 seconds.
What is the timeStamp meant to be doing, by the way?
Initially I wanted it to point to the position where you click, then move but only when its stationary. Now I want it to turn while its moving like a spaceship would to the new location. The video I posted, is what I'm shooting for.
Ah, alright. In that case, just take out the timeStamp check, and it should work.
I did that. I also updated the webplayer link. Click in a forward position, then click behind it. It goes straight back, and its moving before its even completely rotated in that direction. That's why I'm trying to get it so it makes a "U Turn". A smooth transition while turning around. Sorry for the horrible description.
Ah I see what you mean now. You need to add an acceleration to the direction, so that ins$$anonymous$$d of just instantly switching direction from one point to another, the transformation goes slowly from one direction to the other.
Save the point that the person clicks as a different variable, and calculate the time it takes for the Quaternion to finish rotating. Then calculate the acceleration required to reach the correct x, y, and z components of the direction, and apply them until the Quaternion has finished rotating. This should give the ship a nice looking little curve when changing direction.
Or better yet (and less complicated), just change direction to the transform.forward of the cube. The cube will always go in the direction that it is facing.