- Home /
What's wrong with my script?
I want my camera to rotate sideways (roll) and rotate up and down (pitch). I don't know why my script doesn't work. It lets me do one or the other. But if I roll, then pitch, it does all this wierd stuff and i just don't understand. Here is my script:
var moveSpeed:float
function Update ()
{
if(Input.GetButton("up"))
{
transform.Rotate(transform.right * moveSpeed);
}
if(Input.GetButton("down"))
{
transform.Rotate(-transform.right * moveSpeed);
}
if(Input.GetButton("left"))
{
transform.Rotate(transform.forward * moveSpeed);
}
if(Input.GetButton("right"))
{
transform.Rotate(-transform.forward * moveSpeed);
}
}
What is the intent of this script? Because there might be a better script.
u can say how much u want it to rotate like this
transform.rotate = Quaternion(0,0,0,0);
(x,y,z,w)
and u would want to do some kind of $$anonymous$$athf.Lerp(Time.DeltaTime * speed); to smooth it..
transform.rotation = Quaternion(0,0,0,0) might cause a normalization error because you will divide 0 by 0 yielding Quaternion(NaN,NaN,NaN,NaN) where NaN means Not a Number.
moreover the original post said transform.rotate which doesnt exist either.
Quaternions are not euler angles.
[sorry for any perceived rudeness, I happened to just correct your question.]
I put your answer stuff in my script, and it is doing the same thing. This really just doesn't make any sence to me. Do you think it might be , like, some setting?
I don't know if your problem is already solved, but i'd like to point you to a question i asked a long time ago: http://answers.unity3d.com/questions/50555/simple-rotation.html
The answer has a very clear explanation on how to do rotations with quarternions
Answer by Matt-Downey · Aug 25, 2012 at 12:03 PM
The problem is you are using "Left" and "Right" and a bunch of logical but nonetheless nonexistant buttons.
Press Edit-->Project Settings --> Input and you can add "Left" and "Right" and so on if you so wish and your problem will be solved as long as you assign the buttons.
On the other hand you can do things the conventional way and use Input.GetAxis. There a four premade axes, which are "Horizontal", "Vertical", "Mouse X", and "Mouse Y". This will actually simplify your code.
var moveSpeed:float
function Update ()
{
transform.Rotate(transform.right, Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
transform.Rotate(transform.forward, Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime);
}
And kudos on using all the correct/logical axes for rotation. I think you might have to multiply the Input.GetAxis("Vertical") by -1, but that's it. Very nice code.
Your answer
Follow this Question
Related Questions
How to prevent Z axis rotation ? 1 Answer
i would like to rotate an object from an other objects script 1 Answer
Find left joystick Vector2 away from sprite's up direction. 1 Answer
I could not be more lost (rotation script) 0 Answers
How to make a bone rotate facing towards a given Transform? 0 Answers