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