# Rigidbody.addforce then rigidbody.rotation and movement along transform.forward

Hello,
I am novice with scripting and rigidbody and need some info.

I use rigidbody.addforce to get my gameobject moving.

I then use rigidbody.rotation to change the gameobject orientation (which, of course, changes transform.forward as well).

When I do this the gameobject continues to move along it original trajectory.

How can I get my gameobject to move, at all times and maintain speed, along what I set for rigidbody.rotation (always move along transform.forward)?

My gameobject is a plane (or a car, etc) that the player can turn. Am I possibly going about this the wrong way and should not be using rigibody for objects that change direction under user or script control?

Appreciate any help!

you are aware that that is basically a character controller, what your asking for?

it’s not that your continuing to move along the original traj
it’s that your moving along both.
It’s like in space, you start traveling forward, now if you turn left and start going that way you do start going that way, but because there is nothing to stop your previous movement your going what is now your “right” + your new “forward”.

Is this on the ground? A simple increase in friction would probably right the issue real quick (if the friction is high enough the forces moving the wrong direction would quickly dissipate, much like they actually do for a car, a car can only drift with a large amount of force, that force is quickly overcome by friction)

if it is in the air then drag would be the co efficient to change.
If your using rigidbody the most realistic way to modify movement is through forces and counter forces.

otherwise you can rotate and set velocity

velocity = transform.forward * current_speed

that wouldn’t cause any loss of speed though during the rotation, would instantly set your speed which could be wierd. its not realistic but its quick.

I have working code that does this exact thing for an example.

This was adapted from Angrybots, and I’m leaving out variable declarations cause that’s obvious. They’re floats and vector3s. You need to add a frictional cross force proportiortional to the difference between the forward vector and the velocity vector somewhere else if you want ‘realistic’ drag. I don’t have that here but I can post another example if you want that.

``````Top section is from PlayerMoveController Update(), bottom from FixedUpdate of FreeMovementMotor from Angrybots demo and modified.

thrust += Input.GetAxis ("Vertical");
thrust = Mathf.Clamp(thrust, -maxthrust, maxthrust);
turn += Input.GetAxis ("Horizontal") ;

turn = Mathf.Clamp(turn, -maxturn, maxturn);

motor.facingDirection = Quaternion.Euler (0, turn / 60, 0) * motor.facingDirection;
//character.rotation = Quaternion.Euler (0, turn / 60, 0);
motor.movementDirection = motor.facingDirection * (thrust/60);

// Make sure the direction vector doesn't exceed a length of 1
// so the character can't move faster diagonally than horizontally or vertically
if (motor.movementDirection.sqrMagnitude > 1)
motor.movementDirection.Normalize();

function FixedUpdate () {

// Handle the movement of the character
var targetVelocity : Vector3 = movementDirection * walkingSpeed;
var deltaVelocity : Vector3 = targetVelocity - rigidbody.velocity;
if (rigidbody.useGravity)
deltaVelocity.y = 0;

// Setup player to face facingDirection, or if that is zero, then the movementDirection
var faceDir : Vector3 = facingDirection;
if (faceDir == Vector3.zero)
faceDir = movementDirection;

// Make the character rotate towards the target rotation
if (faceDir == Vector3.zero) {
rigidbody.angularVelocity = Vector3.zero;
}
else {
var rotationAngle : float = AngleAroundAxis (transform.forward, faceDir, Vector3.up);
rigidbody.angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
}

}

// The angle between dirA and dirB around axis
static function AngleAroundAxis (dirA : Vector3, dirB : Vector3, axis : Vector3) {
// Project A and B onto the plane orthogonal target axis
dirA = dirA - Vector3.Project (dirA, axis);
dirB = dirB - Vector3.Project (dirB, axis);

// Find (positive) angle between A and B
var angle : float = Vector3.Angle (dirA, dirB);

// Return angle multiplied with 1 or -1
return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);
}
``````