So I’m in the process of learning Unity 5 and scripting in it. One thing I wanted to give a try was to make a simple weapon sway script like you see in games such as battlefield or csgo. After a bit of documentation I finally got a script going (see below).
using UnityEngine;
using System.Collections;
public class WeaponSway : MonoBehaviour {
float mouseX;
float mouseY;
float movementX;
float movementY;
float smooth = 3;
public float speed;
Vector3 defaultposition;
Vector3 newPos;
// Use this for initialization
void Start () {
defaultposition = transform.localPosition;
}
// Update is called once per frame
void Update () {
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
movementX = mouseX * Time.deltaTime * speed;
movementY = mouseY * Time.deltaTime * speed;
Quaternion final = Quaternion.Euler(defaultposition.x * movementX, defaultposition.y * movementY, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation,final,Time.deltaTime*smooth);
}
}
However, for some reason the simple model of the weapon I’m using changes rotation and the weapon doesn’t sway like intended. I can’t see the problem in the script.
Here is a gif of what is happening: Imgur: The magic of the Internet