- Home /
transform.rotation and localRotation automatically reset after rotating.
I was trying to make a camera that's relative to the character, for a robot combat game like Zone of the Enders, Project Nimbus, Strike Suit Zero/Infinity, etc. and I can't get rotation/localRotation to work properly.
float hor = Input.GetAxis("Mouse X") * smooth;
float ver = Input.GetAxis("Mouse Y") * smooth;
/*
if(!target.IsGrounded())
target.transform.Rotate(-ver, hor, 0);
else
target.transform.Rotate(0, hor, 0);
*/
/*
if(!target.IsGrounded())
target.transform.localRotation = Quaternion.Euler(-ver, hor, 0);
else
target.transform.localRotation = Quaternion.Euler(0, hor, 0);
*/
The target is a GameObject. The grounding is irrelevant. If I use Rotate, it rotates 'alright', but not how I want to. If I use rotation/localRotation, it rotates, but only for 1 frame, then returns to the default position. transform.eulerAngles does the same thing. Even if I make an empty game object that is supposed to just exist and spin something by its local rotation, it will not.
I am pretty new to Unity, not new to coding so it doesn't make sense to me how this doesn't work. Please help.
Answer by aernbau · Nov 23, 2015 at 03:10 PM
Don't you love it how you find the answer only after trying to ask someone else?
I've made 2 extra floats to add on the mouse movement. Pretty sure I'll find out I've done something wrong on some other part, but for now...
float hor_mouse = Input.GetAxis("Mouse X");
float ver_mouse = Input.GetAxis("Mouse Y");
hor += hor_mouse;
ver += ver_mouse;
//testtarget.transform.Rotate(-ver, 2, 0);
testtarget.transform.localRotation = Quaternion.Euler(-ver,hor,0);
Yay.
I have a similar problem and I kind of know what is happening but don't know what to do about it. can you help me out with this one?
//for rotation(this code is inside the update function):
newposition = rb.position;
Vector3 velocity = newposition - lastposition;
lastposition = rb.position;
Vector3 direction = velocity.normalized;
Quaternion lookrotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
rb.rotation = lookrotation;
I want the object to face the direction of its movement and it does while it is moving but i want it to stay after it has stopped moving and not return to its default y=0 rotation. Please help.
Your answer
Follow this Question
Related Questions
How to make the character move and rotate where the camera is facing (3rd person) 0 Answers
How to get JUST the y rotation of an object? 3 Answers
Using gamepad triggers to control 3rd person camera 2 Answers
How to move player in direction where the camera is aiming ? 1 Answer
child lookat another child 1 Answer