• 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 twoface262 · Apr 02, 2012 at 09:21 PM · javascriptrotationaienemy

Rotating on X and Z axis only

How would I make this -

 var rotation = Quaternion.LookRotation(player.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damp);


only rotate the object on the X and Z axis? 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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by rutter · Apr 02, 2012 at 09:50 PM

The simplest thing would be if your two objects are on the same plane.

Barring that, you can at least pretend they are by zeroing out the depth axis before calculating your goal rotation:

 var displacement = player.position - transform.position;
 displacement.z = player.position.z; //pretend objects are at same height
 var rotation = Quaternion.LookRotation(displacement); //ed: thanks to Bunny83

By some clever vector multiplication, you could even make that axis-agnostic, but that's probably better left for another day.

If you need more control than that, I suppose you could try something like this:

  • Get a rotation from Slerp()

  • Convert that rotation to its vector form using its `eulerAngles` property

  • Zero out one or more axes

  • Convert that back into a rotation using `Quaternion.Euler()`

Also remembers that if you're using rigidbodies, you can set rotation and position constraints locking them on a particular axis, which is sometimes very handy for 2D simulation.

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 twoface262 · Apr 02, 2012 at 09:58 PM 0
Share

Doesn't seem to work. Either ways. Can't I just see if the player and enemy are on the same Y axis? and it they aren't then not to move the enemy. I just don't know how to go about doing that.

avatar image Bunny83 · Apr 02, 2012 at 10:06 PM 1
Share

I guess you meant Quaternion.LookRotation(displacement);

Look rotation takes a direction vector, not a position.
Beside that this question has been asked several times before.

avatar image twoface262 · Apr 02, 2012 at 10:18 PM 0
Share

Hmm... Has there been a solution? I'm thinking of making a system that checks to see if they are both on the same Y axis if not a bit off from each other. If they aren't then the enemy will follow a set path until the player goes within it's position. Then it checks to make sure they are on the same Y axis and if they are it goes to attack. I just don't see if that would be such a great idea. How would I make an if statement that gets the player.position.y and tranform.position.y then checks to see if they are 3 - 4 meters within the same y axis?

avatar image rutter · Apr 03, 2012 at 12:36 AM 0
Share

@Bunny83: Ah, good catch. Fixed with an edit.

avatar image
1

Answer by DracoScorpius · Apr 02, 2012 at 09:46 PM

A solution, if the object this applies to has a Rigidbody, is to freeze the rotation on the Y axis under constraints.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to Rotate on one axis? 4 Answers

Enemy not rotating when inside range 1 Answer

How to make the enemy move back to its waypoint after it's target killed? 2 Answers

Collisions not working when player stands still 1 Answer

Rotating enemy object to face the player when moving 2 Answers


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