- Home /
How can I keep my z-axis perpendicular to a target location while simultaneously rotating around the x-axis
I would appreciate any help in solving this issue.
Assume up is the y-axis before continuing.
Say I have 2 cubes, A and B. Lets assume B is constantly moving randomly on the (x,z)-plane and A is trying to catch B. Like a cat and a mouse.
A needs to rotate towards B (in other words, A needs to face B) then move towards B. As B is constantly moving A will also have to keep adjusting its rotation to keep facing B.
This can be done with
A.rotation = Quaternion.LookRotation(B.position - A.position);
A.position = Vector3.MoveTowards(A.position, B.position, speed * Time.deltaTime);
This is all fine and good but what if I wanted A to perform some rotating action while chasing B? I can't because LookRotation changes every rotation axis which would erase any changes I make to As rotation.
For example if I want A to chase B and I also want A to turn towards B while constantly rotating about its x-axis. How could I do this?
Sorry if this is not clear, maybe the following examples will clear things up.
A cat chasing a mouse, the line connecting the cat's shoulders (or the cat's z-axis) is always perpendicular to the mouse but the cat's x-axis changes because the cat is doing front flips while chasing the mouse.
A talking ball with eyes and ears is chasing a different mouse. The line connecting the ball's ears is always perpendicular to the mouse (i.e. the ball is facing the mouse) but the ball can only move by rolling so its x-axis must change as it follows the mouse.
With these 2 examples hopefully my question, "How can I keep my z-axis perpendicular to a target location while simultaneously rotating around the x-axis," makes more sense.
Thanks for any help.
Ultimately, I could not find a solution for this issue so ins$$anonymous$$d, I found a workaround. I placed my rolling object into a parent container. The rolling object just rolls while the parent container is in charge of pointing to and moving to the desired locations.
Answer by lgarczyn · Dec 21, 2019 at 08:41 PM
Quaternion.LookRotation
is very powerful if used right.
The first argument is the axis you always want z to be aligned with, and the other is the direction you want x to try to look toward.
A.rotation = Quaternion.LookRotation(Vector3.forward, B.position - A.position);
will work just fine.
Thank you! I just tested this and this is definitely a piece of the puzzle. $$anonymous$$y player isn't rotating correctly as it moves but at the very least it is now rotating as it moves as opposed to just flat out not working. I probably can get it to work today. Thanks again, I was completely stuck before your help.
Edit: Oh just kidding, I removed the code for rotating on the x-axis and just left the code you gave me and the cube moves very strangely. It does not turn to face the enemy it just rotates oddly.
Is the cube a rigidbody? Does the cube have a rotating parent?
Not a rigid body. Basically I was trying to make a cube that constantly spun forward (in the direction of travel) that also rotated towards a target location using LookRotation without altering its x angle or it would disrupt its forward spin. I wanted to find some way to do this on a single object but I kept getting gimbal lock issues after 90 degrees of spinning forward.
Ultimately, I just went ahead and split the rotations into 2 objects. An invisible parent object that takes care of traveling and facing the target location and the child cube object which just spins. It works now.
Your answer
Follow this Question
Related Questions
Combining 2 camera systems 0 Answers
Help with Character Controller 1 Answer
(2d in 3d) Movement relative to camera using JS 0 Answers
How to Logically Match Ground Slope While Using This Code? 1 Answer
Rotating Character 1 Answer