How do I make a cube fly along a single axis.

In my game I am trying to make it so that when a cube hits a player the player gets knocked off the platform. I have looked at a few ways to do this but I would like to try and use the rigidbody.AddForce function. this is the code I have so far:

#pragma strict

//var rocket : Rigidbody;
// speed = 10.0;

var numEnemies : int = 3;

function Update () {

    for(var i : int = 0; i < numEnemies; i++)
    {
        rigidbody.AddForce(Vector3.forward*500,ForceMode.Acceleration);
    }

    //var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation);

    //rocketClone.velocity = transform.forward * speed*1;

}

The code works but only once and then stops. so I tried to add a for loop but it does not work :frowning: can anyone help please?

Thanks

1 Answer

1

Rather than “AddForce”, I suggest that you merely add to the rigidbody.velocity. If you do use AddForce, you should probably use ForceMode.Impulse.

for(var i : int = 0; i < numEnemies; i++)
{
   
   rigidbody.velocity = Vector3.forward * 500;

   // OR

   rigidbody.AddForce(Vector3.forward*500,ForceMode.Impulse);
}