var recoilMod : Transform;
var weapon : GameObject;
var maxRecoil_x : float = -20;
var recoilSpeed : float = 10;
var recoil : float = 0.0;
function Update() {
if(Input.GetMouseButtonDown(0))
{
//every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
recoil+= 0.1;
}
recoiling();
}
function recoiling() {
if(recoil > 0)
{
var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
// Dampen towards the target rotation
recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
recoil -= Time.deltaTime;
}
else
{
recoil = 0;
var minRecoil = Quaternion.Euler (0, 0, 0);
// Dampen towards the target rotation
recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
}
}
the problem is the recoil effect everytime i clicking on my left mouse, and it’s moving just one time, but i want that it will move backward and back to the orginal position and then backward and back to the orginal position and then backward and back to the orginal position, a lot of times…
how can i do that?
…
the recoil happed every time i clicking on the left mouse button, just one time,
No matter I press the button for a long time, it happens only once.
I want it to happen with a single click, a lot of times.
You’re over thinking this way to much. I would send a simple message to your camera look script to increase the y rotation when you fire a shot and then simply return to the original rotation using the provided unity functionalities such as lerp. Hope this helps.
Obviously its clear in your mind what you want, for some reason, its not clear for me Anyways, i suppose you want your weapon recoil after shot, and prevent player to shoot again until the weapon is back to aiming position? You need flag to check if recoil animation is done:
var recoilMod : Transform;
var weapon : GameObject;
var maxRecoil_x : float = -20;
var recoilSpeed : float = 10;
var recoil : float = 0.0;
function Update() {
if(Input.GetMouseButtonDown(0) waitForAni == false)
{
//every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
recoil+= 0.1;
waitForAni = true;
}
recoiling();
}
private var waitForAni:boolean = false;
function recoiling() {
if(recoil > 0)
{
var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
// Dampen towards the target rotation
recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
recoil -= Time.deltaTime;
}
else
{
recoil = 0;
var minRecoil = Quaternion.Euler (0, 0, 0);
// Dampen towards the target rotation
recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
}
if (recoilMod.rotation == minRecoil) waitForAni = false;
}
I am not sure if its correct way to check when the rotation is back to normal like i did in example:
recoilMod.rotation == minRecoil
If that doesnt work, you need to figure out a way to check when your weapon is back to normal rotation. Hope you get at least some new thoughts.
again, i want to recoil(gun moving forward and backward) when i’m shooting.
now the problem is, If the fire button is still pressed, the gun moving forwrd and backward just once until the next click, but i want to make that it will move forward and backward not just once but a lot until i will stop the pressing…
now, what you can’t understand…?