- Home /
Rotate and transform in local space
I've got a problem with a controller i'm trying to code. The idea is, that the object faces and moves in the direction the joystick is pointing. It works as long as there is no camera movement/rotation because the whole movement of the controller is oriented at world space. I have no idea how to change that and need help.
This is my code so far.
if (left_horizontal != 0.0 || left_vertical != 0.0) {
var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
transform.rotation = (Quaternion.Lerp (heroTransform.rotation, Quaternion.AngleAxis(90.0 - angle, Vector3.up), Time.time * (force * moveSpeed)));
controller.Move(transform.forward * force * moveSpeed);
}
Is it the rotation or the translation that's giving you trouble? Your translation call ("move forward") looks more or less okay.
It's not quite clear to me how you want this thing to rotate. If you just want to take joystick input, you'd probably use transform.Rotate. Does that do what you need? If not, why not?
Thanks for your reply! Right now it is not working like it should be because the object movement (rotation and translation) is always in world space. That means if i change my camera angle and position the movement doesn't fit with the perspective seen on screen. I'm not really good at explaining the problem?!
Answer by robertbu · Aug 08, 2014 at 03:44 PM
I'm fuzzy on the details of what you want, but here is an educated guess:
if (left_horizontal != 0.0 || left_vertical != 0.0) {
var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
var fwd = Camera.main.transform.forward;
fwd.y = 0.0;
var q = Quaternion.FromToRotation(transform.forward, fwd) * transform.rotation;
var q = Quaternion.AngleAxis(angle, Vector3.up) * q;
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotationSpeed);
conroller.Move(transform.forward * force * moveSpeed);
}
If your current code is working correctly, there are still aspects that don't make sense or are wrong. For example, I don't know what 'heroTransform.rotation' is, and the use of Time.time in the Lerp() is wrong.
'rotationSpeed' is float you need to define and assign. Start with a value of 5.0.
Thank you very much ! That did the trick. I left out any declarations of variables in my first post, so sorry for the confusion. I'm trying to wrap my $$anonymous$$d about your code and to understand what I did wrong but so far i'm just happy it works.
Answer by zarawesome · Aug 08, 2014 at 12:07 PM
Well, you have to use the camera's vectors instead of global ones. Start by replacing
Vector3.up
with
Camera.main.transform.TransformDirection (Vector3.forward);
You might want to try TransformDirection with Vector3.up or Vector3.right instead, depending on which way your camera was originally oriented in.
Thank you for your reply ! But unfortunately that didnt change anything of the behaviour. The object still moves in world space ins$$anonymous$$d of being relative to the camera.
EDIT: After giving it some thought I think the problem lies in the calculation of the rotation with the controller joystick. I'm not sure how to convert THAT into local space.
Your answer
Follow this Question
Related Questions
Rigidbody constraints in local space? 7 Answers
Physics in Local Space rather than World Space 2 Answers
acceleration and eulerAngle problem 0 Answers