- Home /
How to add weapon sway?(c#)
Ya.... Thanks(c#)
Answer by chrisall76 · Mar 03, 2013 at 09:25 PM
Here, this one handles rotation left/right
using UnityEngine;
using System.Collections;
public class WDragRotation : MonoBehaviour {
float amount = 0.02f;
float maxamount = 0.03f;
float smooth = 3;
private Quaternion def;
private bool Paused = false;
void Start (){
def = transform.localRotation;
}
void Update (){
float factorX = (Input.GetAxis("Mouse Y")) * amount;
float factorY = -(Input.GetAxis("Mouse X")) * amount;
//float factorZ = -Input.GetAxis("Vertical") * amount;
float factorZ = 0 * amount;
if(!Paused){
if (factorX > maxamount)
factorX = maxamount;
if (factorX < -maxamount)
factorX = -maxamount;
if (factorY > maxamount)
factorY = maxamount;
if (factorY < -maxamount)
factorY = -maxamount;
if (factorZ > maxamount)
factorZ = maxamount;
if (factorZ < -maxamount)
factorZ = -maxamount;
Quaternion Final = Quaternion.Euler(def.x+factorX, def.y+factorY, def.z+factorZ);
transform.localRotation = Quaternion.Slerp(transform.localRotation, Final, (Time.time * smooth));
}
}
}
This one allows weapons to rotate on the X axis.
sing UnityEngine;
using System.Collections;
public class WDragMove : MonoBehaviour {
public float amount = 0.02f;
public float maxamount = 0.03f;
public float smooth = 3;
private Quaternion def;
void Start (){
def = transform.localRotation;
}
void Update (){
float factorZ = -(Input.GetAxis("Horizontal")) * amount;
//float factorY = -(Input.GetAxis("Jump")) * amount;
//float factorZ = -Input.GetAxis("Vertical") * amount;
//if (factorX > maxamount)
//factorX = maxamount;
//if (factorX < -maxamount)
//factorX = -maxamount;
//if (factorY > maxamount)
//factorY = maxamount;
//if (factorY < -maxamount)
//factorY = -maxamount;
if (factorZ > maxamount)
factorZ = maxamount;
if (factorZ < -maxamount)
factorZ = -maxamount;
Quaternion Final= Quaternion.Euler(0, 0, def.z+factorZ);
transform.localRotation = Quaternion.Lerp(transform.localRotation, Final, (Time.deltaTime * amount) * smooth);
}
}
what do i add this to? the weapon? camera? it doesnt work with the weapon or camera.....
Add to the weapon, then play around with the amount and maxamount values.
Hello chris; I tried testing this script on a quick gun model I thrown together in Blender. I parented the gun to the fps camera and applied both scripts to the gun model. I then adjusted the values but there's no difference that happens, no matter how high or low the value is.
Oh, turns out I had to uncomment the commented out if statements and the comment out code below each one. Additionally you typed float factor Z twice, so I changed the first into factor X ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
C# Swing Weapon Curve 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer