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
3
Question by DanMarionette · Dec 20, 2010 at 09:56 PM · rotationrigidbodyprojectileweaponfacing

How can I rotate a rigid body based on its velocity?

Hi,

I have a tank that can fire a projectile(rigidbody) at an angle and force specified by the player. A pretty basic artillery game mechanic. The projectile fires in the correct direction but at the moment, left to the physics engine, the projectile(which is a capsule at the moment) doesn't rotate or "point" in the direction of travel as it moves through the air.

Does anyone know how I can achieve this effect?

Thanks

UPDATED 27/12/10

TankControl.cs (attached to the tank - projectile instantiation Method) shellSpawn is an empty gameobject that is a child of the tank gun barrel to spawn the projectile from.

void Fire() { //set the position and rotation of the projectile the the shell spawn point Vector3 position = shellSpawn.position; Quaternion rotation = shellSpawn.rotation;

 GameObject firedProjectile = (GameObject)Instantiate(shell, shellSpawn.position, shellSpawn.rotation);

 firedProjectile.rigidbody.AddRelativeForce(new Vector3(0,1,0) * gunPower, ForceMode.Impulse);

}

Projectile.cs (attached to projectile prefab - Update Method)

void Update () {
    transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
}
Comment
Add comment · Show 1
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 BigDaddyVT · May 05, 2017 at 09:13 PM 0
Share

Worked in Unity 5.6. below is my class. I just added the new script to the Shell prefab and applied.

 using UnityEngine;
 
     public class ShellDirection : $$anonymous$$onoBehaviour
     {
         // Update is called once per frame
         void Update()
         {
             Rigidbody shellRigidbody = gameObject.GetComponent<Rigidbody>();
             gameObject.transform.rotation = Quaternion.LookRotation(shellRigidbody.velocity);
         }
     }
 
 

3 Replies

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

Answer by Mickydtron · Dec 20, 2010 at 10:33 PM

transform.rotation=Quaternion.SetLookRotation(velocity);

This will set your rotation in the exact same direction that you are moving in. This assumes that you have a Vector3 velocity which you are using. It seems a fair assumption, all things considered.

Comment
Add comment · Show 6 · 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 DanMarionette · Dec 27, 2010 at 04:59 PM 0
Share

Thanks for the reply, sorry it's been a while getting back to you but Christmas has taken over for the last week.

I have added what you have suggested to a script attached to the projectile prefab and it does look like its "trying" to do what I want but for my setup it isn't quite working. The projectile twists in the air. It's like it's rotating on two axis not just the one it should.

I have added the code to my question.

avatar image Mickydtron · Dec 27, 2010 at 10:49 PM 0
Share

I'm not sure what you're saying it's doing. so it currently points its nose in the direction of travel, but also spins around its own z axis? Is there any other code that could be affecting the rotation?

avatar image DanMarionette · Dec 30, 2010 at 05:24 PM 0
Share

I fixed the problem. It was due to non-uniform scaling on the prefab. With your answer it works perfectly now. Thanks.

avatar image Pivetta · Jun 19, 2019 at 05:05 PM 0
Share

Quaternion.SetLookRotation() is now a void method and works with this syntax:

 transform.rotation.SetLookRotation(velocity);
avatar image xxmark71xx Pivetta · May 15, 2021 at 05:25 PM 0
Share

Thank you. This works for me!

avatar image bionicartsmobile · Jun 17, 2020 at 07:39 AM 0
Share

This answer is great, but I have an extra question.

I want to

 transform.Rotate(-Input.acceleration.z, 0, 0);

AND Rotate towards rigid.velocity

 transform.rotation = Quaternion.LookRotation(rigid.velocity, Vector3.up);

How can I do both?

avatar image
2

Answer by Bryan-Legend · May 01, 2015 at 06:35 PM

Here's the code that worked for me:

 transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
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 Elson · Aug 18, 2014 at 07:32 PM

//rotates rigidbody to face its current velocity public void RotateToVelocity(float turnSpeed, bool ignoreY) {
Vector3 dir; if(ignoreY) dir = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z); else dir = rigidbody.velocity;

     if (dir.magnitude > 0.1)
     {
         Quaternion dirQ = Quaternion.LookRotation (dir);
         Quaternion slerp = Quaternion.Slerp (transform.rotation, dirQ, dir.magnitude * turnSpeed * Time.deltaTime);
         rigidbody.MoveRotation(slerp);
     }
 }
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

find the angle to rotate the gun to face mouse point with its barrel 1 Answer

can I know What side of my rigidbody is facing down? 1 Answer

Objects Instantiating at wrong position 3 Answers

How to rotate gameobject and rigidbody? 2 Answers

Click button to fire weapon. 3 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