Smooth transition between two Vectors?
I'm writing a script to aim down the sights of a gun. So far I have:
private Vector3 oldPos;
private Vector3 newPos;
void Start ()
{
oldPos = new Vector3 (this.transform.localPosition.x, this.transform.localPosition.y, this.transform.localPosition.z);
}
void Update ()
{
if (Input.GetMouseButtonDown (1))
{
Aim ();
}
if (Input.GetMouseButtonUp (1))
{
UnAim ();
}
}
void Aim ()
{
Vector3 newPos = new Vector3 (0f, this.transform.localPosition.y, this.transform.localPosition.z);
this.transform.localPosition = newPos;
}
void UnAim ()
{
this.transform.localPosition = oldPos;
}
Which works perfectly, but I want the transition back and forth to be smooth. I tried Vector3.Smoothdamp but it messes up my positions and causes the gun to go above the player's head and sometimes gets it stuck there. Is there another way to do this? Or am I implementing incorrectly?
Thanks!
Edit - "a" to "am"
Answer by luisquid · Mar 28, 2017 at 02:25 PM
Have you tried using Vector3.Lerp?
Yes but couldn't seem to figure out how to implement it. Could you give or link an example?
Your answer
Follow this Question
Related Questions
Moving an array of Vector3 with the GameObject (Path-System) 0 Answers
Cannot implicitly convert type `UnityEngine.Vector3' to `UnityEngine.Transform 1 Answer
Unique list of Vector3's 1 Answer
Get Vector3s in range 1 Answer
how to save positions in Vector3 Array und move playerobjekt to the saved position in array's?? 1 Answer