• 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 JP22 · Mar 02, 2011 at 10:49 PM · rotate

How can I make a missile face the direction it's moving?

Hi,

I've been trying to work out what I think is a really simple problem - But I just can't solve it.

I have a missile that's being launched (a simply cylinder default model). The missile is given a Z and Y velocity, so it fires and arcs correctly under gravity.

I want the missile to face the direction it's moving, so it rotates depending on its velocity.

I've tried using lookat but it doesn't seem to work - There's an added problem that if it does work, the original cylinder model is 90 degrees out (i.e, the original model will look like a vertical pillar if fired forwards, rather than a horizontal, er..sausage..)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Peter G · Mar 02, 2011 at 11:03 PM

Here are 2 tricks:

  • Create an empty parent for your rocket. Then rotate the rocket so that it is correctly orientated inside the parent. Then instantiate an instance of your parent when you fire the rocket. This will solve the 90 degree rotation problem.

  • The easiest way to get a missile (parent gameObject) to arc is to have it face its velocity. So, assuming that your missile is like the majority in the world it will face up on the way up, and down on the way down.


    transform.rotation = Quaternion.LookRotation(rigidbody.velocity);

    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 PrimeDerektive · Mar 03, 2011 at 02:04 PM 0
    Share

    That last bit is brilliantly simple, I never thought of that.

    avatar image NatX · Aug 21, 2011 at 05:36 PM 0
    Share

    Parenting the empty object was the key. Thank you!

    avatar image
    1

    Answer by efge · Mar 02, 2011 at 11:04 PM

    Make your meshes be centered on the world origin in your 3D app.
    You can also move the center of mass with this function: Rigidbody.centerOfMass

    Fix the 90 degree rotation: How do I fix the rotation of an imported model?

    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 NatX · Aug 21, 2011 at 05:37 PM 0
    Share

    He's not using an imported model.

    avatar image
    1

    Answer by GalZohar · Jan 01, 2013 at 03:07 AM

     missile.transform.foward = missile.rigidbody.velocity;
    
    

    Or better, use AddTorque to slowly (or quickly, depending on how much torque you add) to rotate the missile towards its velocity. To make it more realistic, make sure you scale the torque you add based on the off-axis velocity, as in:

     float sideVelocity = missile.rigidbody.velocity - Vector3.Dot(missile.rigidbody.velocity, missile.transform.forward))
    
    

    If you just rotate the missile directly, it will be affected too heavily by gravity.

    It may also be a good idea to manually script drag in the direction perpendicular to the movement (sideVelocity *= (1 - dragSide); and then adjust the velocity itself accordingly).

    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 JP22 · Mar 03, 2011 at 12:19 PM

    Thanks for the quick responses!

    I went for Peter G's solution - Worked perfectly, so thanks again.

    I've really gotta get my head around all of the different lookat / LookRotation type function...

    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 Statement · Mar 03, 2011 at 12:31 PM 2
    Share

    Then I suggest you accept his answer to give him some deserved rep. :)

    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

    2 People are following this question.

    avatar image avatar image

    Related Questions

    Camera rotation around player while following. 6 Answers

    Double axis rotation mixing. 1 Answer

    Lock Z Rotation. Character Controller. 1 Answer

    how do I make a joint that fixates the objects distance but lets them rotate freely? 1 Answer

    Camera rotate with mouse in TPC 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