- Home /
 
Need Help Aiming Smoothley
So I have been playing around with some Javascript to try and get my gun to smoothly aim, but I can't seem to get it to move from HipPos to AimPos smoothly. Any help would be awesome!
Here's the Script:
 var HipPos : Vector3;
 var AimPos : Vector3;
 var TimeDamp = 3;
 
 private var MainCam : GameObject;
 
 function Start(){
     //setting the HipPos at the game start
     transform.localPosition = HipPos;
     MainCam = GameObject.FindGameObjectWithTag("MainCamera");
 }
 function Update(){
     if(Input.GetMouseButtonDown(1)){
         //if right click move the gun to main cam
         transform.localPosition = Vector3.Lerp(AimPos, HipPos, TimeDamp / 150);
         //change FOV to align sights correctly
         MainCam.camera.fieldOfView = 20;
     }
     if(Input.GetMouseButtonUp(1)) {
         // if right click is released return it to HipPos
         transform.localPosition = Vector3.Lerp(HipPos, AimPos, TimeDamp / 150);
         //put the camera's FOV back to 60
         MainCam.camera.fieldOfView = 60;
     }
 }
 
              Answer by Dracorat · Oct 30, 2013 at 08:46 PM
The third parameter of Lerp basically means "What percent of this Lerp should I calculate?" with 0.0f meaning "starting point" and 1.0f meaning "ending point". Thus, 0.5f is "Half way between" etc.
As such, you need to need to update the percent each frame according to your desired animation speed - something like percentDone += (1 / timeToAimSeconds) * Time.deltaTime and then use percentDone as the final parameter. (You'll -= instead of += when putting it away). You also will need to clamp the variable to never go below 0.0f or above 1.0f. 
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How does scaleing work? 2 Answers