- Home /
How do I add force on an object(with a rigidbody) that I've randomly instantiated along the "Y" axis?
I know I need to use rigidbody.AddForce, But I'm having trouble finding how. This is what I have, but when I start the game, it spawns the object randomly but doesn't add and force to it. Any help would be greatly appreciated.
var asteroid : Transform;
var asteroidSpeed : float = -200.00;
var spawnAsteroid : boolean = false;
var ran1 : float;
var transPos : float;
function Update (){
if(spawnAsteroid == true){
if(Input.GetButtonDown("Jump")){
var Asteroid : Transform;
asteroid = Instantiate(asteroid, transform.position, transform.rotation);
asteroid.rigidbody.AddForce(transform.right * asteroidSpeed);
}
}
}
Answer by IgorAherne · Jul 18, 2013 at 07:02 PM
var asteroid : GameObject; //you are probably dragging an object into slot on inspector. Use GameObject type variable instead of Transform
// MAKE SURE that rigid body component in present on the object that was dragged from the inspector on the asteroid variable, since we won't be able to affect it's rigid body from FixedUpdate otherwise.
var newAsteroid : GameObject //you want to instantiate an object later, producing a game object. You want to keep in touch with this one. This is why you need a variable of type GameObject.
var asteroidSpeed : float = -200.00;
var spawnAsteroid : boolean = false;
var ran1 : float;
var transPos : float;
function Update(){
if(spawnAsteroid == true){
if(Input.GetButtonDown("Jump")){ //GetButton will allow object to get pushed while pressed; you had getButtonDown, which only accelerated object for split second
newAsteroid = Instantiate(asteroid, transform.position, transform.rotation); //instantiate an object and store its data in newAsteroid. //You must keep in mind that newAsteroid only holds information about one object. If Instantiate is called again, the previous object will be re-written with a new one, and will no longer be accessible via this newAsteroid.
//we are using newAsteroid to add force to the newly instantiated object's rigid body. MAKE SURE that rigid body component in present on the object that was dragged from the inspector on the asteroid variable, since we won't be able to affect it's rigid body from FixedUpdate otherwise.
}
}
}
function FixedUpdate(){
if(Input.GetButton("Jump")){
newAsteroid.rigidbody.AddForce(0,asteroidSpeed,0); //add forces only in fixed update.
}//this function is called 50 times a second by default, as opposed to Update(), which is called much more times per sec
}
Answer by homer_3 · Jul 18, 2013 at 06:05 PM
You've made a local var, Asteroid, which you never use. I've found saving an instantiated game object into the same var it was instantiated from causes weird behavior. spawnAsteriod is also initialized to false, so your instantiate will never get called.
Your answer
Follow this Question
Related Questions
A sphere moved by relativeforce is acting wierd? 2 Answers
rigidbody.AddForce to another object 2 Answers
Rigidbody.Addforce not working in Unity 5.4.1 3 Answers
Declaring myRigidbody 1 Answer
AddForce not working 0 Answers