How to change the gun from Position A to B and again B to A ?
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 it`s initial position! Is there anyone who have solution for my problem. It`s urgent ! :) Thanks in advance ! Sorry for Messy English !
Answer by TrainWrek · Aug 09, 2017 at 06:33 AM
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);
}
}
Answer by UDN_b5c3e1d7-2c45-4d88-8c12-61a9c2e55780 · Aug 11, 2017 at 06:49 AM
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);
}
}
Answer by · Aug 10, 2017 at 03:46 AM
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 !
Try changing transform.position to transform.localPosition, and gun.position to gun.localPosition. This way the gun will move relative to its parent.
Can you give me script as you are saying I tried didnot working :( Help me!