Ok, I have a recoil and gun movement script that has been causing me a bit of trouble. The movement works, but the recoil does not. The recoil pushes the gun back but does not bring it back forward. Also, this isn’t a big issue but the way the gun sways when I move, it seems a bit slow and under exaggerated. If you have any idea what it wrong or how I could fix it, please post your answers. Every answer will be appreciated greatly because I am new to Unity and somewhat a beginner with Javascript. Here is my script:
#pragma strict
var MoveAmount : float = 1.0f;
var MoveSpeed : float = 2.0f;
var gunModel : GameObject;
var cameraModel : GameObject;
var MoveOnX : float = 0.0f;
var MoveOnY : float = 0.0f;
var DefaultPos : Vector3;
var NewGunPos : Vector3;
var DefaultRot : Quaternion;
var NewGunRot : Quaternion;
var ONOFF : boolean = false;
var recoilAmount : float = 0.0f;
var amountToRecoil : float = 0.0f;
var recoilMultiplier : float = 1.0f;
var recoilMax : float = 0.0f;
var recoilMin : float = 0.0f;
var rotationMultiplier : float = 10.0f;
function Awake () {
NewGunRot = DefaultRot;
DefaultPos = transform.localPosition;
ONOFF = true;
}
function Update () {
if(Input.GetMouseButtonDown(1)){
ONOFF = false;
}
if(Input.GetMouseButtonUp(1)){
ONOFF = true;
}
if(Input.GetMouseButton(0)){
recoilAmount += amountToRecoil;
}
if(recoilAmount >= recoilMax){
recoilAmount = recoilMax;
}
if(recoilAmount <= recoilMin){
recoilAmount = recoilMin;
}
if(Input.GetMouseButtonDown(0)){
recoilAmount -= (amountToRecoil / 2);
}
}
function LateUpdate () {
if(ONOFF == true) {
MoveOnX = Input.GetAxis("Mouse X") * Time.deltaTime * MoveAmount;
MoveOnY = Input.GetAxis("Mouse Y") * Time.deltaTime * MoveAmount;
NewGunPos = new Vector3(DefaultPos.x+MoveOnX,DefaultPos.y+MoveOnY,DefaultPos.z - (recoilAmount * recoilMultiplier));
gunModel.transform.localPosition = Vector3.Lerp(gunModel.transform.localPosition, NewGunPos, MoveSpeed * Time.deltaTime);
}
NewGunRot = Quaternion.Euler(DefaultRot.x, DefaultRot.y, DefaultRot.z - (recoilAmount * recoilMultiplier) * recoilMultiplier);
gunModel.transform.localRotation = NewGunRot;
}