- Home /
turning on x and y axis for turret but not using lookat?
i have a model which has a base and body and a gun
base body gunthis is so the body can turn and the gun will inherit from the body and the gun can turn , but i not know the code to target an object .
the look at function as i know it is an instant facing direction , which would snap the model into place which i not want , i would like a smooth motion , any one know of any tutorials for these 2 axis working together
Answer by aldonaletto · Jul 28, 2012 at 11:54 PM
The easiest way is to rotate the body strictly around Y, and let the gun rotate freely - it seems that the gun rotates only around X, and is rotated around Y by the body. Add this script to the base:
var target: Transform; // drag the target here var body: Transform; // drag the body here var gun: Transform; // drag the gun here var speed: float = 5;
function Update(){ var dir = target.position - body.position; var dirH = dir; dirH.y = 0; var rotGun = Quaternion.LookRotation(dir); var rotBody = Quaternion.LookRotation(dirH); body.rotation = Quaternion.Slerp(body.rotation, rotBody, speed Time.deltaTime); gun.rotation = Quaternion.Slerp(gun.rotation, rotGun, speed Time.deltaTime); } Instead of dragging the target to the field Target in the Inspector, you can find it at Start:
function Start(){ target = GameObject.FindWithTag("Player").transform; }