- Home /
Specific use of TransformDirection
Vector3 direction = new Vector3(Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
direction = _mainCamera.TransformDirection (direction);
Vector3 velocity = direction * _moveSpeed;
I have a camera that is offset at an angle roughly 45 degrees around the y, and I want my character controller to move relative to the camera. This code is basically doing what I want it to, and I understand the basic use of TransformDirection, but having two objects referenced is making a local to world conversion into something else.
direction(input.GetAxis) seems to be worldspace. The cameras direction is local(?).
I don't understand what is going on with the _mainCamera.TransformDirection(direction) part. I want to set direction to the cameras direction, great. Why do I reference direction inside the brackets for the cameras TransformDirection?
So what I am (not)understanding about this is that it is setting direction, which is in worldspace, to the worldspace direction of the camera, which is in localspace, which is referencing the worldspace direction of direction.
Where am I going wrong here?
You say, "This code is basically doing what...," so are you just asking how this code works, or is there a specific issue you are trying to solve?
Sorry, I am having a terrible time trying to figure out how to phrase my question. The code works as I assumed it should though it isn't quite right for what I need. I want to understand it better to try and adjust it to work correctly.
All the examples of TransformDirection I can find are just adjusting one localspace to worldspace. I have two objects referenced here, not sure how that plays into what is going on.
But you don't have two objects references here...at lest not in the code above. Input.GetAxis() just gets input from the keyboard, mouse or joystick. This input is not married to any specific object. You turn it into a vector, but that vector is neither local or global. It could be either.
For example, imagine that the value of "Horizontal" is 0.0 and the value of "Vertical" is 1.0. That is the joystick is all the way forward, or you are hitting the up arrow key. 'direction' would be (0.0, 0.0, 1.0) which is the same thing as Vector3.forward. In terms of direction in world space, that is going straight towards positive 'z'. If I use this vector as a local coordinate (as I might with Transform.Translate()), then it represents the forward of the object.
Your line...
direction = _mainCamera.TransformDirection (direction);
...translates the local coordinate acquired form the GetAxis() calls from local to global. So if your input was (0.0, 0.0, 1.0) as above, the 'direction' will become the forward of the camera in world space. That is it will be _mainCamera.forward. If you use your player in this calculated direction, the player will be moved in the direction the camera is facing.
Right I see. I was attributing the direction value to the game object when i should be thinking of it as just a vector. I really appreciate the clarification of the getaxis results too. Thank you very much Robertbu.
Your answer
Follow this Question
Related Questions
how to set the rotation of a game object around the world axes ? (can't use transform.rotate) 1 Answer
Clamping rotation doesn't work when local axis is different from global 0 Answers
How do I make an object move on the world space rather than the local space? 1 Answer
DragRigidbody - move object in Local Space, not World Space 1 Answer