Other
Weapon Sway Script messes up weapon rotation
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: http://imgur.com/zi6w6Om
Quaternion final = Quaternion.Euler(defaultposition.x * movementX, -90, 0); fixes the issue but the gun won't sway on the Y axis.
Answer by JimNero009 · Jun 21, 2016 at 10:00 AM
I'm confused about the quaternion you call final here. Could you output the value of this to the console?
Follow this Question
Related Questions
Slerp Rotating 1 Degree and Limiting All Other Rotation 1 Answer
How do I smoothly rotate the GameObejcts with negative 90 degrees 0 Answers
How do I rotate the GameObejcts smoothly with negative degrees 0 Answers
why the effects of the Post Processing stack do not activate through an if-statement 0 Answers
Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 1 Answer