- Home /
Help with Lerp
Hello!
I want my gun to follow smoothly the FPS camera. I tried to use Lerp but I don't get how it is used... any help?
this is what I tried:k
var gun : GameObject;
var target : GameObject;
function Start() {
}
function Update() {
transform.position = Vector3.Lerp(gun.transform.position, target.position, Time.deltaTime);
}
It should be on your gun, yes. I don't see anything wrong with the script for it to not work. Does your gun move at all? You could apply a speed to it for it to move faster. And also, if your camera is rotating about, which I assume it is, you probably need to follow with rotation as well as position.
#pragma strict
var target : GameObject;
var speed : float = 2;
function Start () {
}
function Update () {
transform.position = Vector3.Lerp(transform.position, target.transform.position, Time.deltaTime * speed );
transform.rotation = Quaternion.Slerp(transform.rotation, target.transform.rotation, Time.deltaTime * speed );
}
Just a note: if the script is attached to your gun, you don't need to have a variable to access the GameObject, you can simply use "gameObject" to access the GameObject your script is attached to, or "transform" to access your GameObject's transform, like above.
It works, but it goes "inside" the camera, so it's not shown. I want it to "follow" the track of the camera, to make the FPS player more realistic..