• 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
Question by CoryRobertson · Aug 19, 2019 at 01:15 AM · rotationphysicsvector3velocityflying

How to set velocity to new direction after rotation?

Hi all. So I've been working on a VR demo where I fly by flapping wings attached to my hands. Each wing adds force forwards/upwards depending on the type of movement. The problem is turning. I've written a RotatePlayer script that uses the wings positioning (one high, one low) to turn. However when my player rotates, the velocity stops almost entirely and then I drop. How do I keep my velocity going in the direction I have rotated?

Here is my script for the rotation. I took some of the code from this:

link text

Here is my code. Obviously ignore the commented out portions. :

 public class RotatePlayer : MonoBehaviour
 {
     public GameObject Player;
 
     public float rotationDirection;
 
     public RightWing RightWing;
     public GameObject RightWingTip;
 
     public LeftWing LeftWing;
     public GameObject LeftWingTip;
 
     public float speed = .3f;
     public float velocityChangeSpeed = 10.0f;
 
     Rigidbody PlayerBody;
 
     // Start is called before the first frame update
     void Start()
     {
         PlayerBody = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         // If both WingCollider sets are active, then get the difference between the wing tips and turn accordingly. 
         if(RightWing.FlightMode.state && LeftWing.FlightMode.state)
         {
             float WingDelta = Mathf.Abs(RightWingTip.transform.position.y - LeftWingTip.transform.position.y);
             if (WingDelta >= 0.75f)
             {
                 print(PlayerBody.velocity);
                 if (RightWingTip.transform.position.y > LeftWingTip.transform.position.y)
                 {
                     float step = Time.deltaTime * speed;
                     Player.transform.Rotate(0.0f, rotationDirection * step, 0.0f);
 
                     print(PlayerBody.velocity);
                 }
                 else if(RightWingTip.transform.position.y < LeftWingTip.transform.position.y)
                 {
                     float step = Time.deltaTime * speed;
                     Player.transform.Rotate(0.0f, -rotationDirection * step, 0.0f);
                     print(PlayerBody.velocity);
                 }
                 DoStandardPhysics();
             }
         }
 
        
 
         if(RightWing.TopColliderRight && LeftWing.TopColliderLeft)
         {
             //PlayerBody.velocity = Vector3.one * 0.3f;
         }
 
     }
 
     public void DoStandardPhysics()
     {
          //Apply the user rotation in a banked turn
         //PlayerBody.rotation = GetBankedTurnRotation(PlayerBody.rotation);
         //Correct our velocity for the new direction we are facing
 
         PlayerBody.velocity = Vector3.Lerp(PlayerBody.velocity, GetDirectionalVelocity(PlayerBody.rotation, PlayerBody.velocity), Time.deltaTime);
 
     }
 
         //Get new yaw and roll, store the value in newRotation
         public Quaternion GetBankedTurnRotation(Quaternion theCurrentRotation)
        {
         //Quaternion getBankedTurnRotation(float curZRot, float curLift, float curVel, float mass) {
         // The physics of a banked turn is as follows
         //  L * Sin(0) = M * V^2 / r
         //    L is the lift acting on the aircraft
         //    θ0 is the angle of bank of the aircraft
         //    m is the mass of the aircraft
         //    v is the true airspeed of the aircraft
         //    r is the radius of the turn    
         //
         // Currently, we'll keep turn rotation simple. The following is not based on the above, but it provides
         // A pretty snappy mechanism for getting the job done.
         //Apply Yaw rotations. Yaw rotation is only applied if we have angular roll. (roll is applied directly by the 
         //player)
         Quaternion angVel = Quaternion.identity;
         //Get the current amount of Roll, it will determine how much yaw we apply.
         float zRot = Mathf.Sin(theCurrentRotation.eulerAngles.z * Mathf.Deg2Rad) * Mathf.Rad2Deg;
         //We don't want to change the pitch in turns, so we'll preserve this value.
         float prevX = theCurrentRotation.eulerAngles.x;
         //Calculate the new rotation. The constants determine how fast we will turn.
         Vector3 rot = new Vector3(0, -zRot * 0.8f, -zRot * 0.5f) * Time.deltaTime;
 
         //Apply the new rotation 
         angVel.eulerAngles = rot;
         angVel *= theCurrentRotation;
         angVel.eulerAngles = new Vector3(prevX, angVel.eulerAngles.y, angVel.eulerAngles.z);
 
         //Done!
         return angVel;
     }
 
 
     //When we do a turn, we don't just want to rotate our character. We want their
     //velocity to match the direction they are facing. 
     public Vector3 GetDirectionalVelocity(Quaternion theCurrentRotation, Vector3 theCurrentVelocity)
     {
         Vector3 vel = theCurrentVelocity;
 
         vel = (theCurrentRotation * Vector3.forward).normalized * theCurrentVelocity.magnitude;
 
         return vel;
     }
 
 }


Comment

People who like this

0 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 brandoncluff · Oct 11, 2020 at 09:09 PM 0
Share

Were you able to solve this?

0 Replies

· Add your reply
  • Sort: 

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

215 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating a moving object 0 Answers

Velocity to 2D rotation (angle from velocity vector with atan2) 1 Answer

(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer

Changing 1 parameter of rigidbody.velocity (Vector3) 2 Answers

Using Lerp in a Constant Moving Object 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