- Home /
Question by
chrisall76 · Sep 14, 2012 at 01:34 AM ·
lerpgunsight
Lerping doesn't return to the same position?
As the title says, when I lerp my gun to a sight position and back, it goes to a different position than it's suppose to. I can't figure out how/why this happens. Any help? Here's my script:
public var DefaultPos : Vector3;
var SightPos : Vector3;
var SightBool: boolean = false;
var Speed : float = 1.00;
var AimPosX : float = 0.00;
var AimPosY : float = 0.00;
var Sight : GameObject;
var Gun : GameObject;
function Start(){
DefaultPos = transform.localPosition;
SightPos = Sight.transform.localPosition;
}
function Update(){
if (Input.GetButtonDown("Fire2")){
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, SightPos, Time.deltaTime * Speed);
}
if (Input.GetButtonUp("Fire2")){
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, DefaultPos, Time.deltaTime * Speed);
}
}
Comment
Best Answer
Answer by Codetastic · Sep 14, 2012 at 08:54 AM
Well there are actually several problems I see here:
//There is no boolean to check if we want to scope in or not (more on that later)
public var DefaultPos : Vector3;
var SightPos : Vector3;
var SightBool: boolean = false;
var Speed : float = 1.00;
var AimPosX : float = 0.00;
var AimPosY : float = 0.00;
var Sight : GameObject;
var Gun : GameObject;
function Start(){
//This should be the guns start position not the transforms. Unless the transform is the gun of course
DefaultPos = transform.localPosition;
SightPos = Sight.transform.localPosition;
}
function Update(){
//This is only called when the button is pushed down so it only runs the lerp for one frame what would be better is setting a boolean to true/false and lerping over time
if (Input.GetButtonDown("Fire2")){
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, SightPos, Time.deltaTime * Speed);
}
//same as above
if (Input.GetButtonUp("Fire2")){
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, DefaultPos, Time.deltaTime * Speed);
}
}
Here is a better way:
var defaultPos : Vector3;
var sightPos : Vector3;
//a boolean to know if we are scoping in or out
var scopeIntoSights : boolean = false;
var speed : float = 1.0;
var aimPosX : float = 0.0;
var aimPosY : float = 0.0;
var sight : Transform;
var gun : Transform;
//the percentage we are scoped in
var scopedInPercent : float = 0.0;
function Start(){
DefaultPos = gun.localPosition;
sightPos = sight.localPosition;
}
function Update(){
//Check if Fire2 was pushed down or up and set boolean accordingly
if (Input.GetButtonDown("Fire2")) scopeIntoSights = true;
else if (Input.GetButtonUp("Fire2")) scopeIntoSights = false;
//Check if we should scope in or out, and if we have already.
//If not done with the transition add or take away accordingly
if(scopeIntoSights == true && !Mathf.Approximately(scopedInPercent,1.0)) scopedInPercent += Time.deltaTime * speed;
else if(scopeIntoSights == false && !Mathf.Approximately(scopedInPercent,0.0)) scopedInPercent -= Time.deltaTime * speed;
//Lerp takes a value between 0 and 1 so clamp to make sure it stays inside 0 and 1
scopedInPercent = Mathf.Clamp(scopedInPercent,0.0,1.0);
//apply position based on percent, default position and sight position
gun.localPosition = Vector3.Lerp(defaultPos, sightPos, scopedInPercent);
}
Hope that works out for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081026im_/https://answers.unity.com/themes/thub/images/avi.jpg)