- Home /
Eulerangles and Transform.forward... something's off?
So, long story short I'm just trying to make a simple first-person-shooter for the moment (adding on stuff to actually make it interesting later on, but not worrying about that for now). I'm sure that a similar question must have been asked some time in the past but I honestly have no idea how to search for my problem. I'm using my mouse's inputs to change the rotation on my camera, and attaching an object for movement. The object's rotation is made to match the camera's rotation, and then when WASD keys are pressed the object moves according to transform.forward and transform.right... however, when I rotate the camera to certain angles sideways, the movements get all screwed up.
I'm sorry if my explanation is bad, if you need to test yourself to tell the specifics of it, the only specifications for this scene is a camera and single object. Thankyou in advance for advance for anyone who can/does help out with this script. I'm sure it's definitely not the most efficient, but I'm just trying to work with what I have.
script for the object
var movespeed:float = 4;
var zecamera:GameObject;
function Start () {
}
function FixedUpdate ()
{
transform.rotation.y = zecamera.transform.rotation.y;
if(Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * movespeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.D))
{
transform.position += transform.right * movespeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A))
{
transform.position += transform.right * -movespeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.S))
{
transform.position += transform.forward * -movespeed * Time.deltaTime;
}
}
script for the camera
var playerlel:GameObject;
var sensitivityY:float = 1;
var sensitivityX:float = 1;
var movespeed:float = 4;
function Update ()
{
//rotate up/down
transform.eulerAngles.y += Input.GetAxis("Mouse X") * sensitivityY * Time.deltaTime;
//rotate left/right
transform.eulerAngles.x -= Input.GetAxis("Mouse Y") * sensitivityX * Time.deltaTime;
transform.position = playerlel.transform.position;
}
Answer by robertbu · Oct 06, 2013 at 03:37 PM
You have a couple of related issues here. First from the script reference for Transform.eulerAngles:
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.
The euler angles you set are not necessarily the ones you get back out. Run this bit of script:
transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
The output is (0,180,180). So imagine you want to set (180, 45, 0), but you set it one axes at a time starting with 'x'. You would get (0, 45, 180)...not even physically the rotation you wanted.
Given your code above, the solution is to maintain your own Vector3 and assign it. Never read the eulerAngles. Something like:
myRotation.y += Input.GetAxis("Mouse X") * sensitivityY * Time.deltaTime;
myRotation.x -= Input.GetAxis("Mouse Y") * sensitivityX * Time.deltaTime;
transform.eulerAngles = myRotation;
Thanks for the answer, I guess my understanding of what eulerangles are and how they work really need some refining :P. I'll test this out as soon as I can get back to working on it, looks like a pretty solid solution to me though. :D
That bit you gave me I think did make my camera's movement smoother, at least probably it won't mess up what I'm planning on doing next. I actually realized that the problem I was having was due to the "player" object that I was trying to attach the camera to (the player object's rotation matches the camera's rotation) and that was making the movement screwy. now that I've figured's that out I can be well on my way :P.
Your answer
Follow this Question
Related Questions
Rotate object around transform.forward 1 Answer
Store initial rotation in JavaScript 2 Answers
Smooth rotation in 90° increments 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Why does my rotation lock? 2 Answers