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.
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