Crawdad
February 15, 2022, 9:07pm
1
I don’t know why I always have so much trouble with this, but I can’t seem to get my character to rotate on his local y-axis based on joystick direction. I’ve been perusing the forums all afternoon trying to find a solution, and nothing I try works properly. I just want the character to look in the direction the joystick is pointing, without loosing his local up direction. Suggestions appreciated. What I have at the moment…
Vector3 lookPoint = transform.TransformPoint(Input.GetAxisRaw("RightStickHor") * 5, 0, Input.GetAxisRaw("RightStickVert") * 5);
Quaternion localRot = new Quaternion();
localRot.SetLookRotation(lookPoint, transform.up);
transform.localRotation = localRot;
It depends on what you mean by “local up direction”. If you’re talking about the up direction defined by its parent Transform, then this will work:
Vector3 lookPoint = new Vector3(Input.GetAxisRaw("RightStickHor"), 0, Input.GetAxisRaw("RightStickVert"));
transform.localRotation = Quaternion.LookRotation(lookPoint);
If you mean that there’s some local z or x axis rotation that you want to preserve… maybe this is the ticket?
Vector3 lookPoint = new Vector3(Input.GetAxisRaw("RightStickHor"), 0, Input.GetAxisRaw("RightStickVert"));
float angle = Mathf.Atan2(Input.GetAxisRaw("RightStickVert"), Input.GetAxisRaw("RightStickHor")) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, transform.up);
1 Like
Crawdad
February 15, 2022, 9:27pm
3
PraetorBlue:
It depends on what you mean by “local up direction”. If you’re talking about the up direction defined by its parent Transform, then this will work:
Vector3 lookPoint = new Vector3(Input.GetAxisRaw("RightStickHor"), 0, Input.GetAxisRaw("RightStickVert"));
transform.localRotation = Quaternion.LookRotation(lookPoint);
If you mean that there’s some local z or x axis rotation that you want to preserve… maybe this is the ticket?
Vector3 lookPoint = new Vector3(Input.GetAxisRaw("RightStickHor"), 0, Input.GetAxisRaw("RightStickVert"));
float angle = Mathf.Atan2(Input.GetAxisRaw("RightStickHor"), Input.GetAxisRaw("RightStickVert")) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, transform.up);
I mean it’s own transform.up. I have this object set as a child of a parent object, so that as the parent rotates, this object’s local rotation maintains zero. I’d like for this object to be able to rotate on it’s local y-axis to look at lookPoint. I tried your solution and unfortunately it did not work for me. Thanks for the interest though. Any other ideas would be appreciated.
EDIT: After re-reading my script example, I realize that some editing I did to make it less confusing might have had the opposite effect. Please see the script below. IT’S ATTACHED TO THE PARENT, and is attempting to ROTATE THE CHILD playerRotation on its local Y-axis to look at lookPoint. Sorry again for the bad initial post.
Vector3 lookPoint = transform.TransformPoint(Input.GetAxisRaw("RightStickHor") * 5, 0, Input.GetAxisRaw("RightStickVert") * 5);
Quaternion localRot = new Quaternion();
localRot.SetLookRotation(lookPoint, transform.up);
playerRotation.transform.localRotation = localRot;
Crawdad:
I mean it’s own transform.up. I have this object set as a child of a parent object, so that as the parent rotates, this object’s local rotation maintains zero. I’d like for this object to be able to rotate on it’s local y-axis to look at lookPoint. I tried your solution and unfortunately it did not work for me. Thanks for the interest though. Any other ideas would be appreciated.
EDIT: After re-reading my script example, I realize that some editing I did to make it less confusing might have had the opposite effect. Please see the script below. IT’S ATTACHED TO THE PARENT, and is attempting to ROTATE THE CHILD playerRotation on its local Y-axis to look at lookPoint. Sorry again for the bad initial post.
Vector3 lookPoint = transform.TransformPoint(Input.GetAxisRaw("RightStickHor") * 5, 0, Input.GetAxisRaw("RightStickVert") * 5);
Quaternion localRot = new Quaternion();
localRot.SetLookRotation(lookPoint, transform.up);
playerRotation.transform.localRotation = localRot;
Then you just need to do this:
Vector3 lookPoint = new Vector3(Input.GetAxisRaw("RightStickHor"), 0, Input.GetAxisRaw("RightStickVert"));
playerRotation.transform.localRotation = Quaternion.LookRotation(lookPoint);
1 Like
Crawdad
February 15, 2022, 10:19pm
5
PraetorBlue:
Then you just need to do this:
Vector3 lookPoint = new Vector3(Input.GetAxisRaw("RightStickHor"), 0, Input.GetAxisRaw("RightStickVert"));
playerRotation.transform.localRotation = Quaternion.LookRotation(lookPoint);
Son of gun, that did it! I don’t know why, but working with rotation always ties my brain in knots. Thanks for the save.