• 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
1
Question by RealSamFisher · May 13, 2014 at 02:50 PM · direction

Rotate object to face movement direction

Hello everybody,

I have a problem in Unity which I don't know how to solve it. I have seen several similar threads already, but I didn't find a solution to the problem. Maybe that's because I don't understand completely how Unity is working.

So I am trying to move an object from position A to position B. The y-coordinate doesn't affect the movement of the object. I have already established a system in which the object moves a certain pattern (a drone searching for the shortest distance to lead a car to a free parking place). Now I have done that and it works fine, but it looks terrible cause the object doesn't turn to the direction it's moving.

So I have tried some methods, like:

drone.transform.localRotation = Quaternion.RotateTowards(drone.transform.localRotation, Quaternion.LookRotation(new Vector3 (entryX, 0.0f, entryZ)), 1000f * Time.deltaTime);

The drone rotates, but not in the direction it's heading. Moreover the rotation affects how the drone is moving further, kinda ruining the aspect how it leads the car to a free parking place.

Is there a way how I can rotate the drone without affecting the movement?

Thanks in advance.

Comment
Add comment · Show 2
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 robertbu · May 13, 2014 at 02:52 PM 0
Share

Please post your drone movement code. In order to figure out the right rotation fix, we need to understand how the rotation is impacting the movement. This may be a model issue rather than a code issue.

avatar image RealSamFisher · May 13, 2014 at 04:40 PM 0
Share

Here is the code for movement:

if (drone.transform.position.x <= x) {

drone.transform.Translate (new Vector3 (speed, 0, 0) * Time.deltaTime);

}

if (drone.transform.position.z <= z) {

drone.transform.Translate (new Vector3 (0, 0, speed) * Time.deltaTime);

}

if ((drone.transform.position.x - x) > 0.1) {

drone.transform.Translate (new Vector3 (-speed, 0, 0) * Time.deltaTime);

}

if ((drone.transform.position.z - z) > 0.1) {

drone.transform.Translate (new Vector3 (0, 0, -speed) * Time.deltaTime);

}

drone.transform.localRotation = Quaternion.RotateTowards(drone.transform.localRotation, Quaternion.LookRotation(new Vector3 (entryX, 0.0f, entryZ)), 1000f * Time.deltaTime);

x and z are the coordinates to which the object needs to move.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jeff-Kesselman · May 13, 2014 at 04:20 PM

Assuming your model faces such that it moves along the forward axis, you can treat the movement as a direction vector.

eg transform.rotation = Quaternion.LookRotation(rigidBody.velocity);

Note that the rotation done this way is a world rotation, not a local rotation

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to determine if enemy is on my left or right hand side? 2 Answers

Making laser beam point in shooting direction C# 1 Answer

AddForce with direction tuning 0 Answers

want to child object facing to camera 2 Answers

shoot in direction of tilt 0 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