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
2
Question by Disaster · May 06, 2010 at 08:51 PM · rigidbody

Rotate object to face direction of movement

Hi, I have a rigidbody which I am moving around in a 2D way using the following code:

void FixedUpdate() {

 float xPos = Input.GetAxis("Horizontal") * _player.playerSpeed * Time.deltaTime;
 float yPos = Input.GetAxis("Vertical") * _player.playerSpeed * Time.deltaTime;

 Vector3 newPos = new Vector3(xPos, 0, yPos);
 _position = _position + newPos;

 if(_position != Vector3.zero)
 {
     _player.rigidbody.MovePosition(_position);
 }

}

It's working fine, but I'm struggling to make it so the gameObject rotates to face the direction in which the object is moving. Most of the code I have found assumes we are wanting to move X, Y and Z where as I am only really interested in X and Z.

If anyone has anyone suggests as to how I can get this functionality I'd be very grateful :)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by gl03 · May 28, 2012 at 08:54 PM

You can simply set the object's blue axis to the rigidbody's velocity Vector3:

 transform.up = rigidbody.velocity;

If you want to use a rigbody...

Comment
Add comment · Show 2 · 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 Gru · Nov 12, 2015 at 03:14 PM 0
Share

Best solution for 2D. transform.up is green axis though.

avatar image AlphaGen · Nov 15, 2016 at 08:51 AM 0
Share

Best OF the Solutions

avatar image
2

Answer by qJake · May 06, 2010 at 09:05 PM

Could you use Transform.LookAt, and then just lock the Y-axis to whatever you need it to be?

Comment
Add comment · Show 1 · 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 St4rKiller070 · Jul 31, 2013 at 11:21 AM 0
Share

how can I lock Y-axis?

avatar image
2

Answer by sean · May 06, 2010 at 09:08 PM

Sorry I can't provide a code sample, I'm still in the "trial-and-error the syntax as I go" stage of Unity-specific javascript, but the theory is basically take the direction you're moving, either pulled from the .x and .z of your velocity or tracked frame to frame as a difference of previously-saved and current x & z position and drop it in a vector. I believe Unity has some LookAt or set-rotation-to-face-vector functions built in, otherwise you're back to using a Mathf.atan2 call and euler rotations.

But Spike's probably got the straightforward built-in: .LookAt where you're going, and then set the pitch component of the object rotation to 0.

Comment
Add comment · Show 1 · 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 joeyrubio · Feb 15, 2011 at 10:20 PM 2
Share

How do I make my GameObject look at where it is going?

avatar image
1

Answer by anivader1789 · Jan 19, 2014 at 03:53 AM

See the top voted answer in this thread

http://answers.unity3d.com/questions/33380/rotate-to-facing-direction.html

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity3D Ai wont shoot 1 Answer

Trouble with OnCollisionEnter and Exit 1 Answer

hit.normal cube 0 Answers

How to keep rotation from switching between - and + after a 360? 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