- Home /
script not working?
So when i tried out this code ( I made about 90% of it myself so of course it wouldnt work ) .... it doesnt work - basicly i want my gun to move to the recoil position x and y when i press fire 1 (left click) but it wont budge..
var originalPosx = -0.1600189;
var originalPosy = -0.2532201;
var recoilPosx = -0.1597214;
var recoilPosy = -0.2532201;
var dampVelocity = 0.1;
var dampVelocity2 = 0.1;
function Update (){
var recoilPosx = Mathf.SmoothDamp(Gun.transform.localPosition.x, recoilPosx, dampVelocity, .3);
var recoilPosy = Mathf.SmoothDamp(Gun.transform.localPosition.y, recoilPosy, dampVelocity, .3);
Gun.transform.localPosition.x = recoilPosx;
Gun.transform.localPosition.y = recoilPosy;
if (Input.GetButtonDown("Fire1")) {
recoilPosx = -0.159721;
recoilPosy = -0.2532063;
//dont know what to do so it resets ... and goes back to its originbal position
recoilPosx = -0.1600189;
recoilPosy = -0.2532063;
}
}
Answer by KellyThomas · Dec 15, 2013 at 09:40 AM
Update will run on every frame, but before each frame is rendered.
Your lines 26 & 27, are overwriting the changes made by lines 21 & 22, before anything is drawn.
Instead you could do this:
if (Input.GetButtonDown("Fire1")) {
recoilPosx = -0.159721;
recoilPosy = -0.2532063;
}
else {
recoilPosx = -0.1600189;
recoilPosy = -0.2532063;
}
But I recommend swapping out the literal numbers and using variables you can modify using the inspector.
Your answer
Follow this Question
Related Questions
Broken code, need help tonight if possible please 1 Answer
How can I get x, y, z spawnpoints from a csv or text file? 1 Answer
2d rigidbody drag on x, y axis 0 Answers
Please help. I can't stop this light from moving!!! 1 Answer
y and z rotation axis doing the same thing when x is 270 degrees 0 Answers