- Home /
Gun Rotation
This script do that when I move the mouse X the gun rotates but i have an error:
Assets/GunMoveMentewfweaf.js(18,33): BCE0024: The type 'UnityEngine.Quaternion' does not have a visible constructor that matches the argument list '(float, float, float)'.
The Script:
public var RotateAmount : float = 1;
public var RotateSpeed : float = 2;
public var GUN: GameObject;
public var RotateOnX : float;
public var RotateOnY : float;
public var DefaultRot : Quaternion;
public var NewGunRot : Quaternion;
function Start()
{
DefaultRot = transform.localRotation;
}
function Update ()
{
RotateOnX = Input.GetAxis( "Mouse X" ) *Time.deltaTime * RotateAmount;
RotateOnY = Input.GetAxis( "Mouse Y" ) *Time.deltaTime * RotateAmount;
NewGunRot = new Quaternion ( DefaultRot.x+ RotateOnX, DefaultRot.y+ RotateOnY, DefaultRot.z);
GUN.transform.localRotation = Quaternion.Lerp(GUN.transform.localRotation, NewGunRot , RotateSpeed * Time.deltaTime);
}
Answer by robertbu · Mar 16, 2013 at 04:56 PM
Quaternions are 4D and the constructor takes 4 parameters, and these parameters are not euler angles. To do what you want here use Quaternion.Euler:
NewGunRot = Quaternion.Euler( DefaultRot.x+ RotateOnX, DefaultRot.y+ RotateOnY, DefaultRot.z);
tnks now works, but when i press "play" my gun rotates 90 Y without move the mouse, help :c
Without understanding more of your project, it is difficult to figure out how to solve your problem. A couple of things. First isolate your gun from it rotation. You can do this by using an empty game object as a parent. Place the empty game object at teh pivot point for your gun. When you have your gun setup like you want, make it a child of an empty game. Apply your rotation code to the empty game object. In addition, you want to apply the X mouse movement to the Y axis rotation and the Y mouse movement to the X axis rotation. You have it opposite in your code.
Answer by nsxdavid · Mar 16, 2013 at 05:02 PM
A Quaternion takes 4 arguments. You are trying to set the Quaternion from Euler angles.
This can be done, though... like this:
public var NewGunRot:Quaternion = Quaternion.identity;;
NewGunRot.eulerAngles = Vector3(( DefaultRot.x+ RotateOnX, DefaultRot.y+ RotateOnY, DefaultRot.z);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Child Obj Ignoring Parents Rotation! Help 1 Answer
Determining Look Rotation As > A Number? 1 Answer
Javascript Compile error 2 Answers
My Code that Works on an object's rotation doesn't work. 2 Answers