- Home /
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 :( can anyone help please?
Thanks
Answer by ThatsAMorais · Nov 26, 2013 at 10:31 PM
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613122852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
rb.AddForce problem 2 Answers
Rigidbody add force relative to any rotation 1 Answer
How to have a cube to fall on fixed rotation? 1 Answer
Rigidbody falling very slow 1 Answer
Grenade throw with indicator 0 Answers