- Home /
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:
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;
}
}
Your answer
Follow this Question
Related Questions
How do I determine which Euler Axis of an object will change when the object is rotated? 1 Answer
Game Object rotation sometimes doesn't match defined quaternion 0 Answers
Add force problem 1 Answer
Ideas for keeping a rigidbody's rotation aligned with parent axes? 0 Answers
Rotating a moving object 0 Answers