- Home /
LookAt rotate around X
I know this has been asked and answered a million times , mostly relating to y rotation for turrets etc. but.....
I am trying to make a Cube (1) Rotate around its X Axis only. To look at another Cube object (creating a Flap ). Local Z is facing forward correctly ,Y is Up. Using below simple code, Cube (1) snaps to look at World Z
targetPostition = new Vector3 ( transform.position.x, target.position.y, target.position.z );
transform.LookAt(targetPostition);
If I set it to :
... new Vector3 ( target.position.x, **transform.position.y**, target.position.z );
Cube(1) stays facing forward in local position and rotates perfectly around Y only, but I need to rotate around X.
I have been through tons of answers here and elsewhere , have also tried using Quaternion.LookRotation , but again with .x restricted to transform.position . Cube(1) looks towards World Z only.
Im well and truly stuck here, any suggestions greatly appreciated
Answer by Hellium · Jan 16 at 07:53 PM
private void Update()
{
targetPosition = transform.position + (Vector3.ProjectOnPlane(Target.position - transform.position, transform.right));
transform.LookAt(targetPosition);
}
This will keep the rotation of the object on its Y and Z intact while rotating the object along its X axis.
NOTE: targetPosition apologies for my mis spelling !
Thank you very much for replying This only seems to work if the target cube is moved into a position so that Cube(1) is again looking in World Z direction.
ProjectOnPlane () has fried my brain , especially after spending all day frustrated with what i though would be a simple process.
i just want to rotate the long cube like a wing flap around x only ( will be making target a grabbable object in XR Interaction.
If there is a more suitable solution please let me know .
Thanks everyone ))
I've updated the script and fixed an issue when the object wasn't at the origin, I hope it will fix the issue you're having.
The LookAt
method is meant to align the Z
axis towards the target position.
Fantastic. That works perfectly. I hate to just paste stuff in without knowing how it works , but now i can get over this hurdle and onto the next one.
Thank you ever so much for your help
Your answer
Follow this Question
Related Questions
Rotate pieces of model to look at player 1 Answer
Unity Scripts is disabled 1 Answer
Teleporting? 0 Answers