I’m trying to change the position of my weapon to switch between hipfire/ADS
if(Input.GetMouseButtonDown(1)){
if(!ADS){
ADS = true;
transform.localPosition = Vector3.Slerp(HipPos, AdsPos, 200);}
else if(ADS){
ADS = false;
transform.localPosition = Vector3.Slerp(AdsPos, HipPos, 200);}}
It is working perfectly except that it’s not slerping, no matter what number I enter(So don’t mind the ‘200’). Does anybody have any idea why it is not slerping to it’s new position?
Thanks in advance
Its hard to ignore the 200 because that it telling it to go to the end of the Slerp. Slerp goes from 0 to 1 so anything above 1 would be the final position. This would make your slerp really just an assignment to the final position.
edit: the third parameter value goes from 0 to 1. with 0 being completely the from vector and 1 being to vector.
First Slerp() is for spherical movements, so I hope that is what you are trying to do here. Second, as @Adamcbrz mentions, the final parameter in a Lerp() needs to be between 0 and 1, and often needs to change from 0 to 1 over time. Slerp() just calculates a new position once. It does not cause any motion over time. So you have to execute it over time. Input.GetMouseButtonDown() only executes for a single frame, so your Slerp() is only being called once.
Here is a bit of untested code to show you how you might restructure your logic. Note that the Slerp() gets executed every frame.
#pragma strict
var hipPos : Vector3;
var adsPos : Vector3;
private var endPos : Vector3;
private var ads = true;
var speed = 1.0;
function Start () {
endPos = AdsPos;
}
function Update () {
if(Input.GetMouseButtonDown(1)){
ads = !ads;
if(asd)
endPos = adsPos;
else
endPos = hipPos;
}
transform.localPosition = Vector3.Slerp(transform.localPosition, endPos, Time.deltaTime * speed);}
}