Addforce still adding force after destroyed script

i have a problem with addforce right now. I made a script that controls a cube using the accelerometer on the phone so if the phone tilts left the block goes left etc, well my problem is when it hits the ground i destroy the script, but the block keeps adding as much force as it was before the script was destroyed. so if it was going right before it was destroyed it would keep going that way at the same speed forever. Mind you i need physics to work as well.

Code:[syntax=“javascript”]function OnCollisionEnter2D(coll: Collision2D) {
//Destroy the script for the controller after he hits the ground(So he can’t move the cube after he hits the ground)
GameObject.Find(“Player”).rigidbody2D.AddForce(Vector2.up);
//Adding gravity back to the cube after he hits the ground(Gravity is at 0 before he hits the ground)
rigidbody2D.gravityScale = 0.53;
Destroy (this);

}

var forceVec: Vector2;
var hue: int = 0.1;

function FixedUpdate () {
var dir : Vector3 = Vector3.zero;

//Moving players x by the rotation of the phone
rigidbody2D.AddForce(Input.acceleration.x * forceVec);

rigidbody2D.AddForce(Vector3.down * 1.50);

}[/syntax]

Do you understand what “AddForce” does?
Reset your object’s velocity and it will stop moving.

But then the cube wouldn’t bounce and obey the velocity it had. Also i just started using unity a little while ago so it’s probably a novice mistake.

Let me explain it a bit farther. I have a cube i want to bounce and obey the velocity it gained through the rigidbody2D.AddForce(Input.acceleration.x * forceVec); so if it was moving left at a good speed i want to keep the velocity gained from that upon touchdown but not let it give anymore velocity than it already has. I’m not sure if there is a way to make it so you can’t keep adding anymore force after it collides. I’m sorry if this doesn’t make much sense.

  1. How come you’re creating a “dir” Vector3 every physics step, that’s equal to zero?

  2. You aren’t assigning your forceVec variable anything,

  3. AddForce is something that builds upon existing velocity, so it will increase in power the longer it is happening, if no other force is counteracting it.

4 What exactly are you trying to achieve here? Is this like, some enemy your player falls on and it bounces them up?

I was testing some stuff with dir not too long ago. The same with forceVec. Is there any other option besides add force for rigidbody2D? constantforce only work with a normal rigidbody. I’m trying to achieve a stacker like game using physics instead of just placing the block.

AddForce doesn’t continue to function if it’s not being called, so there must be something else going on with how you’re approaching it, is what I was getting at. I’m still not quite sure I understand what you’re trying to achieve with the adding of velocity/force, perhaps you could give us a visual example?

Ok so i just tried it again and for whatever reason is works how it’s supposed to. I didn’t even change the fucking code and it worked. I’ve been stuck on this for days and all of a sudden it just works… But i still haven’t gotten around to testing it on an actual device so here’s to hoping it works there to. Also here is what the ugly test bed looks like http://imgur.com/6VSlUha

Ok this makes a lot more sense. Your post made it sound like the AddForce was continuing to push the cube after it hit, not that it actually just wasn’t doing anything for you. A better description would have been “why isn’t AddForce adding force…” haha. AddForce needs much larger values than rigidbody.velocity, if you want it to actually negate the velocity of something. If you want the cube to bounce off the platform by a set amount, just set the velocity directly like this:

rigidbody2D.velocity = new Vector2(player.rigidbody2D.velocity.x, bounceUpPower);

Also, when working with the 2D system, you only need to use Vector2’s most of the time, since we’re only working with the X and Y axis.