Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by RicardoAntezana · Mar 26, 2016 at 04:28 PM · rotationquaternionrotation axistransform.rotationlookrotation

Rotating an Object (To Face Another Object) Only on X and Y Axis

Hello, I did some research on the topic and I couldn't understand any answers so I decided to ask here. I would like my character to face the object, here is that part of the script:

 function lookAt ()
 {
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }

The problem comes when "Target" gets to close to this object and it leans slightly back to deal with the Y axis, I thought if it would only face the objects X and Z axis it might fix this. Any help is appreciated. Thanks.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Geometrical · Mar 26, 2016 at 07:57 PM

Hello, the answer is pretty simple:

 function lookAt() {
     // Store the other object's position in a temporary variable
     var temp = Target.position;
     // Deflate it's x and z coordinate
     temp.x = temp.z = uint.MinValue;

     var lookRotation = Quaternion.LookRotation (temp);
     transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Damping * Time.deltaTime);
 }
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 RicardoAntezana · Mar 27, 2016 at 05:02 AM 0
Share

This somewhat works, but he faces the wrong way, the target is my player so it is constantly moving and cannot set the angle to anything specific, how would I go about with this?

avatar image Geometrical · Mar 27, 2016 at 03:44 PM 0
Share

@SociopathStudios I've edited my answer, try again.

avatar image RicardoAntezana Geometrical · Mar 27, 2016 at 06:30 PM 0
Share

It does not work again, the object only face upwards now

avatar image Geometrical RicardoAntezana · Mar 27, 2016 at 07:10 PM 0
Share

What's the forward axis on your player's model/object? X, Y, or Z? Only use the forward axis to get the direction, make the other axis 0.

avatar image
2

Answer by Alrick · Mar 27, 2016 at 08:13 PM

did you try something like:

transform.LookAt(new Vector3(Target.position.x, Target.position.y, transform.position.z));

OR

Target.LookAt(new Vector3(transform.position.x, transform.position.y, Target.position.z));

Depends on what should look at what.

Comment
Add comment · 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
0

Answer by TobiasW · Mar 26, 2016 at 04:55 PM

Untested, but this should work:

 var delta = target.position - transform.position;
 var angle = Math.Atan2(delta.y, delta.x) * Mathf.Rad2Deg;
 var rotation = Quaternion.Euler(0, angle, 0);
Comment
Add comment · Show 8 · 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 RicardoAntezana · Mar 26, 2016 at 05:28 PM 0
Share

Would I put all of that under my lookAt function? How would I intergrate this to my script?

avatar image RicardoAntezana · Mar 26, 2016 at 05:35 PM 0
Share

Also, it somewhat works, but he faces the wrong way.

avatar image TobiasW RicardoAntezana · Mar 27, 2016 at 12:36 AM 0
Share

Yeah, it would just replace "var rotation = Quaternion.LookRotation(Target.position - transform.position);"

And depending on how exactly the way is wrong, try using "angle+180" or "-angle" in Quaternion.Euler.

avatar image TobiasW RicardoAntezana · Mar 27, 2016 at 12:40 AM 0
Share

Alternatively, this should also do it:

  function lookAt ()
  {
      var delta = Target.position - transform.position;
      delta.z = 0;
      var rotation = Quaternion.LookRotation(delta);
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
  }
avatar image RicardoAntezana TobiasW · Mar 27, 2016 at 05:00 AM 0
Share

Oh yes I see where you may be confused, the Target is my player that is constantly moving around, and I cannot set a specific angle as it may change once the player moves.

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

49 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

Related Questions

Rotation and Gravity Relative to the Center of an Object 0 Answers

Delayed LookAt, but only on one axis 1 Answer

How to find a position's angle around an arbitrary axis? 1 Answer

transform.rotation spazes out 0 Answers

how to point the positive Y axis of an object to a direction 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