Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
4
Question by Richard 3 · Feb 09, 2011 at 07:36 AM · movementaddforcedirectionforceface

Face forward direction of movement

I am trying to get an object like an arrow to point in the direction it is moving. I am using this code from the Roll a Ball tutorial. Any ideas?

public var force:float = 1.0; public var simulateAccelerometer:boolean = false;

function Start() {

 iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Landscape;

}

function FixedUpdate () { var dir : Vector3 = Vector3.zero;

 if (simulateAccelerometer)
 {

     dir.x = Input.GetAxis("Horizontal");
     dir.z = Input.GetAxis("Vertical");
 }
 else
 {

     dir.x = -iPhoneInput.acceleration.y;
     dir.z = iPhoneInput.acceleration.x;


     if (dir.sqrMagnitude > 1)
         dir.Normalize();
 }

 rigidbody.AddForce(dir * force);

}

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
19
Best Answer

Answer by runevision · Feb 09, 2011 at 12:09 PM

Use this:

transform.rotation = Quaternion.LookRotation(dir);

Note that if you make the object always face in the direction of movement this way, it won't be rolling anymore (of course) so that may affect the physics if you were relying on a rolling behaviour.

You could instead still have a round collider that is rolling, and have your visible object be a child object of that, and use the LookRotation function on that child, so the visible child (without a collider) is pointing in the direction of movement while the invisible parent (with a collider) is rolling freely.

Edit: Actually, you may want to use this to avoid very fast rotations, or errors if the object is standing still:

if (dir != Vector3.zero) {
    transform.rotation = Quaternion.Slerp(
        transform.rotation,
        Quaternion.LookRotation(dir),
        Time.deltaTime * rotationSpeed
    );
}
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 Richard 3 · Feb 09, 2011 at 02:04 PM 0
Share

This is exactly what I needed. Thanks

avatar image pikpiak · Jul 18, 2011 at 06:30 AM 0
Share

Is there anyway to maintain it's last faced direction even after no Input?

avatar image
4

Answer by Stardog · Jan 20, 2013 at 09:48 AM

Here's another solution that will look in the direction the ball's moving, not the direction of input. Good for attaching a hat/gun to the ball or something.

 transform.position = ballGameObject.transform.position;
 transform.LookAt(transform.position + ballGameObject.rigidbody.velocity);

  • Position this object to where the ball is.

  • Add the ball's "rigidbody.velocity" to this object's position, and then look at it. (The velocity value gives a point offset from the center that you can look at.)

Now just make your arrow model a child of this GameObject. Move the Z position out as far as you want the arrow to appear.

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 shough · Apr 05, 2018 at 03:28 AM 0
Share

Great solution! Works perfect for hats

avatar image
0

Answer by joeyzhao2018 · Nov 15, 2021 at 02:19 AM

In my case I'm working on a top-down 2D game and I need to rotate my bullet. The code is

 transform.rotation = Quaternion.LookRotation(Vector3.forward, shootDirection);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why is force only being added in the same direction? 1 Answer

How to fix/clamp rigidbody dir vector, despite Force dir 1 Answer

Have a character face their moving direction? 1 Answer

Separating direction from force w/ rigidbody.AddForce 1 Answer

My object can't move left and right but only jump? 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