- Home /
Question by
vanzandtlg · Apr 30 at 01:39 PM ·
rotationrigidbodyvelocity
Adjusting Roll and Pitch with Velocity
I'd like the roll and pitch of my submarine to react to changing direction. Going back-and-forth changes the pitch; side-to-side changes the roll.
I have the following code that lerps between a x and z rotation as a function of x and z velocity
xVelocity = rigidbody.velocity.x;
yVelocity = rigidbody.velocity.y;
zVelocity = rigidbody.velocity.z;
maxXVelocityPercentage = Mathf.Abs(xVelocity / maxXVelocity);
maxYVelocityPercentage = Mathf.Abs(yVelocity / maxYVelocity);
maxZVelocityPercentage = Mathf.Abs(zVelocity / maxZVelocity);
activeXAngle = Mathf.Lerp(0, maxXAngle, maxXVelocityPercentage);
activeZAngle = Mathf.Lerp(0, maxZAngle, maxZVelocityPercentage);
if (xVelocity > 0)
activeXAngle = -activeXAngle;
if (zVelocity > 0)
activeZAngle = -activeZAngle;
transform.localEulerAngles = new Vector3(activeZAngle, 0f, activeXAngle);
Works great when the sub is straight in the scene, however, if not, then the sub starts reading any movement as contributing towards the z velocity and giving me weird rotations.
Is there a way to localize velocity? Or a better way of doing what I'm trying to do?
Thanks!
Comment