I am using Unity 5.6.1
I have model of gun and I want to do that, if I press the Right Shift Key the Gun position get change to
Vector X, Y and Z and as I release the key the Gun again smoothly comes to its initial position! Is there anyone who have solution for my problem. It
s urgent !
Thanks in advance !
** Sorry for Messy English ! **
You could try to modify the gun position over time.
public float lerpSpeed = 1.5f;
public Transform gun;
public Vector3 onKeyPos;
Vector3 origPos;
void Start() {
if (gun == null) {
Debug.LogWarning('Gun transform is null, the component has been disabled.');
enabled = false;
return;
}
origPos = gun.transform.position;
}
void Update() {
if (Input.GetKey(KeyCode.RightShift)) { //Move the gun towards the onKeyPos
gun.position = Vector3.Lerp(gun.position, onKeyPos, Time.deltaTime * lerpSpeed);
}
else {
gun.position = Vector3.Lerp(gun.position, origPos, Time.deltaTime * lerpSpeed);
}
}
You could try to modify the gun position over time.
public float lerpSpeed = 1.5f;
public Transform gun;
public Vector3 onKeyPos;
Vector3 origPos;
void Start() {
if (gun == null) {
Debug.LogWarning('Gun transform is null, the component has been disabled.');
enabled = false;
return;
}
origPos = gun.transform.position;
}
void Update() {
if (Input.GetKey(KeyCode.RightShift)) { //Move the gun towards the onKeyPos
gun.position = Vector3.Lerp(gun.position, onKeyPos, Time.deltaTime * lerpSpeed);
}
else {
gun.position = Vector3.Lerp(gun.position, origPos, Time.deltaTime * lerpSpeed);
}
}
Sorry, It didnot work for me I put the value of origPos and OnKeyPos and add gun to Gun`s transform after that when play game and press shift key the gun goes far away from camera and as I release key it comes just behind the camera ! Is there any solution for problem ! And Yes I add these script in WeaponHolder which is empty Gameobject and contains one more script of sway !