Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lramos_unity · Dec 21, 2019 at 08:39 PM · rotationmovement

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.

  1. 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.

  2. 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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lramos_unity · Dec 22, 2019 at 01:02 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lramos_unity · Dec 21, 2019 at 09:24 PM 0
Share

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.

avatar image lgarczyn lramos_unity · Dec 22, 2019 at 06:42 PM 0
Share

Is the cube a rigidbody? Does the cube have a rotating parent?

avatar image lramos_unity lgarczyn · Dec 22, 2019 at 06:54 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges