- Home /
How to rotate a gameobject over time to face another
here's my code at the moment:
function lookAt (){
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
With this code my enemy space ship turns to face me (target variable), but it does this almost instantly, which isn't really very good considering it's turning a whole spacecraft. I need a way to make this rotation happen over time, this amount of time should be able to be altered by a variable rotateSpeed. I understand why this happens instantly, because I'm setting the objects rotation to face the object, not altering it over time every frame, but I can't think of how to do it. I know Javascript but I'm new to Unity.
hi what is the value of damping
set it somewhere to 1.08f
You code here looks fine. Quaternion.Slerp() does rotate over time. This use will result in an eased movement at the end of the rotation. Change 'damping' to rotateSpeed and set appropriately to the speed you want to rotate. If you don't want an eased movement, use Quaternion.RotateTowards() in place of Slerp().
yes, lookAt() is called every frame, but I'm gonna try out Travis' code then get back to you, but what I did was i played my project and moved the player with the scene editor and the enemy turned to face the player extremely quickly,shouldI just divide the Time.deltaTime by damping ins$$anonymous$$d of multiplying?
Time.deltaTime is how much of a second has passed since the last frame to get smooth results you should always use that. So if you want the look at to take 5 seconds its Time.deltaTime/5
Answer by Travis-Bulford · Jul 01, 2013 at 04:23 PM
You need to break your code into two parts.
The part that sets the direction you want to face and the part that turns towards that direction over time, and lets assume that the target object might move while we trying to look at it. Sorry code in c# should be easy enough to translate to js
GameObject lookGameObject=null;
// Set target
public void lookAt (GameObject target){
lookGameObject=target;
}
public void Update(){
// Various update code
if (lookGameObject!=null){
lookAtRotation = Quaternion.LookRotation(lookGameObject.position - transform.position);
// Will assume you mean to divide by damping meanings it will take damping seconds to face target assuming it doesn't move
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime/damping);
// Add code here to set lookGameObject to null if facing angle is close enough to our goal.
}
}
Afraid this was edited here could be code issues
thx for code, please refer to my comment on making the time not fixed but depend on how much the object rotates, is there a way to make the time it takes to complete the turn based on the amount the object has to turn ins$$anonymous$$d of fixed as the damping variable
You might have missed my above comment.
This might be what you need
Ok so what you want is a speed that it will rotate at over time until it faces the direction you want not a time based rotation.
Try looking at
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html
It allows all the same properties but includes a speed factor.
Ok, could you please write a small piece of sample code showing me how to use SmoothDamp in this situation. Preferably using the variable rotation from the code in the OP and the variable damping to deter$$anonymous$$e speed, looking at the page you directed me to I can't quite understand how, do I just use the same code as in the OP but use SmoothDamp ins$$anonymous$$d of Quaternion.Slerp? I don't quite understand. Thanks
Your answer
Follow this Question
Related Questions
Rotate GameObject Issue 1 Answer
Rotate 90 over time on mouseDown 2 Answers
Trying to delay movement of a GameObject 1 Answer
How do I follow a gameobject with the camera 3 Answers
Rotating a turret body and head 1 Answer