- Home /
Problem with rigidbody rotation
hello friends, first of all I must tell you that I'm new to unity I am, I'm doing a script in C# that controls the movement of an Osprey i'm using a model with rigidbody I think is a more realistic alternative. I show a little code:
for elevation: rigidbody.velocity = new Vector3(0, elevation, 0);
for rotation: rigidbody.MoveRotation(Quaternion.Euler(ospreyEuler));
Now the problem arises here when I want to apply a force forward or backward: rigidbody.AddRelativeForce(transform.forward * verticalSpeed, ForceMode.Impulse);
once rotated model the forward vector object is not updated so that upon application of the force moves in what was forward before rotation ie if there is a rotation in the osprey 90 degrees to the right and apply the force for forward the osprey is moving to the left and vice versa.
Answer by elecs · Dec 26, 2013 at 11:13 PM
the error was in freeze position settings, had blocked the freeze position x the code was like this:
// turning
horizontalMovement = Input.GetAxis("Horizontal");
ospreyPitch = Mathf.Lerp(ospreyPitch, -30 * horizontalMovement, Time.deltaTime);
Vector3 ospreyEuler = rigidbody.rotation.eulerAngles;
ospreyEuler.z = ospreyPitch;
if (horizontalMovement > 0)
{
rotationSpeed = Mathf.Lerp(rotationSpeed, maxRotationSpeed, Time.deltaTime);
ospreyEuler.y += Time.deltaTime * rotationSpeed;
}
else if (horizontalMovement < 0)
{
rotationSpeed = Mathf.Lerp(rotationSpeed, maxRotationSpeed, Time.deltaTime);
ospreyEuler.y -= Time.deltaTime * rotationSpeed;
}
else
{
rotationSpeed = Mathf.Lerp(rotationSpeed, 0, Time.deltaTime *10f);
}
rigidbody.rotation = Quaternion.Euler(ospreyEuler);
Vector3 targetVelocity = new Vector3(0, elevation, verticalSpeed);
targetVelocity = transform.TransformDirection(targetVelocity);
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
rigidbody.AddForce(velocityChange, ForceMode.Impulse);
thanks!
Answer by robertbu · Dec 26, 2013 at 06:41 PM
You either want:
rigidbody.AddRelativeForce(Vector3.forward * verticalSpeed, ForceMode.Impulse);
or:
rigidbody.AddForce(transform.forward * verticalSpeed, ForceMode.Impulse);
'transform.forward' is a world coordinate, not a local coordinate.
Are you rotating before you are applying force? You cannot apply force and then rotate.
You understand that applying new force does not cancel out the existing momentum of the object. If you have a ship in space moving forward, applying a side force turns the ship somewhat, but does not give it an immediate 90 degree turn. If this is the issue here, you can try a couple of things. First, you can up the Rigidbody.Drag(). Or for an immediate turn, you can directly assign to Rigidbody.velocity rather than use AddForce().
maybe the error is here?
horizontal$$anonymous$$ovement = Input.GetAxis("Horizontal");
osprey$$anonymous$$ch = $$anonymous$$athf.Lerp(osprey$$anonymous$$ch, -30 * horizontal$$anonymous$$ovement, Time.deltaTime);
Vector3 ospreyEuler = rigidbody.rotation.eulerAngles;
ospreyEuler.z = osprey$$anonymous$$ch;
if (horizontal$$anonymous$$ovement > 0)
{
speedRotation = $$anonymous$$athf.Lerp(speedRotation , maxSpeedRotation , Time.deltaTime);
ospreyEuler.y += Time.deltaTime * speedRotation;
}
else if (horizontal$$anonymous$$ovement < 0)
{
speedRotation = $$anonymous$$athf.Lerp(speedRotation , maxSpeedRotation , Time.deltaTime);
ospreyEuler.y -= Time.deltaTime * speedRotation ;
}
else
{
speedRotation = $$anonymous$$athf.Lerp(speedRotation , 0, Time.deltaTime);
}
rigidbody.rotation =Quaternion.Euler (ospreyEuler);