Having trouble transferring sideways: motion to forwards motion.

Hi all!

Just wondering if anyone can help me with this physics problem, I have a really simple rigid body object that I am moving around a scene and I’d like to be able to transfer the sideways motion to forwards motion to sort of steer it around the place.
My approach isn’t right though, I’m grabbing the sideways motion and just adding that to forwards motion so it always speeds the object up as it steers. Which seems to work at 90° angles but anything else just builds up speed.

If anyone out there has a chance could you look at my script? I’m not great with physics.
I’d love to get my head around this a little more so I can control the physics a bit more.
I also uploaded an asset if anyone feels like checking the scene out.
http://www.peterleary.com/ForumFiles/physics%20Test%202-c3OlCgE0Bl.unitypackage

Thanks!
Pete

public class KeyboardInput : MonoBehaviour {

    public Rigidbody RB;
    public Transform RotationParent;
    public float Speed;
    public float RotationSpeed;
    public Vector3 LocalVelocity;
   
    void Update () {
        if (Input.GetKey("w"))MoveIt(1);
        if (Input.GetKey("s"))MoveIt(-1);   
       
        if (Input.GetKey("d"))RotateIt(1);   
        if (Input.GetKey("a"))RotateIt(-1);   
       
        if (Input.GetKeyDown("c"))RotateAmount(45);   
        if (Input.GetKeyDown("z"))RotateAmount(-45);
    }
   
    void FixedUpdate(){
//Get the Local Velocity
        LocalVelocity = RotationParent.InverseTransformDirection(RB.velocity);
        float SidewaysVelocity = -LocalVelocity.x;//Grab any sideways velocity
        float ForwardVelocityAdd = Mathf.Abs(LocalVelocity.x);//This will be added to forward motion
        RB.AddForce(RotationParent.TransformDirection(new Vector3(SidewaysVelocity,0,ForwardVelocityAdd)),ForceMode.VelocityChange);
    }
   
    public void MoveIt(int Direction){
        float SpeedMultiplied = Speed * Time.deltaTime * Direction;
        Vector3 LocalForce = RotationParent.TransformDirection(new Vector3(0,0,SpeedMultiplied));
        RB.AddForce(LocalForce,ForceMode.Force);
    }
   
    public void RotateIt(int Direction){
        float RotationSpeedMultiplied = RotationSpeed * Time.deltaTime * Direction;
        RotationParent.Rotate(new Vector3(0,RotationSpeedMultiplied,0) );
    }
   
    public void RotateAmount(float RotateAmount){
        RotationParent.Rotate(new Vector3(0,RotateAmount,0) );
    }
}

Thanks,
Pete

A force would need to be added at a normal to the direction of motion change the direction only. You can calculate the acceleration needed to maintain velocity with v^2/r, where v is the velocity and r is the radius of the path, but it’ll take some more calculations to figure out an approximate r.

If you don’t want to do that, it’s probably easiest to just use something like this just after rotating the object:

Rigidbody.velocity = transform.forward * Rigidbody.velocity.magnitude;

This will change only the direction of velocity.

Thanks Hyblademin,
I was avoiding setting the velocity directly because I have heard it can do bad things but the alternative seems like it might be a bit to complicated for me so I guess i’ll just try that and see how I go.
P.