x


AddForce wont apply force to gameobject...help please

I am created a top-down shooter and i have the player rotate towards the mouse...i have a script attached to the player to instantiate a grenade to be thrown when the mouse button is clicked. The grenade shoots BUT just drops right to the ground, no force is applied once its instantiated...here is my code...

var speed = 40;
var cratePrefab:Transform;

//static var canShoot = false;

function Update()
{
    if(Input.GetButtonDown("Fire1"))
    {
        //only shoots if player has ammo
        if(Collisions.GRENADE_AMMO > 0)
        {
        Collisions.GRENADE_AMMO -= 1;
        //GameObject.Find("g_count").guiText.text=""+Collisions.GRENADE_AMMO;
        var crate = Instantiate(cratePrefab,GameObject.Find("shootpoint").transform.position,Quaternion.identity);
        crate.rigidbody.AddForce(Vector3.forward * speed);
        }
    }
}

all help is greatly appreciated!!

more ▼

asked Oct 21 '10 at 06:57 PM

John 17 gravatar image

John 17
17 4 4 10

Ensure that the crate rigidbody is not kinematic.

Oct 21 '10 at 07:23 PM VS48
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Make sure that your crate is not colliding with the one throwing it. You can use Physics.ignoreCollision or make sure to offset it to avoid that problem.

If that's not it, assuming your cratePrefab has a non-kinematic rigidbody and depending on your scale, your speed is probably too low to either be noticeable or to overcome your drag or other factors. Forces don't actually represent speeds directly, but force applied and that's why they have to be much larger. With a force of 40, your object should achieve a speed of 4 units forward initially. Try something like 400 or 4000. If you want to force the initial speed and direction, you could also use crate.rigidbody.velocity to set the speed explicitly.

more ▼

answered Oct 21 '10 at 07:09 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

I got it to actually SHOOT using velocity, but now its only moving in the Z direction no matter where i am aiming. I'm new to unity and i understand why its doing that but how can i change this:

crate.rigidbody.velocity = Vector3(0,0,speed);

so that the velocity is applied to wherever the player is facing...something like transform.position...

Oct 21 '10 at 07:18 PM John 17

maybe i can use local coordinates instead of Vector3??

Oct 21 '10 at 07:21 PM John 17

The velocity Vector3(0,0,something) would indicate a movement along the world z axis by something. To get a local direction, you would either use crate.rigidbody.velocity = crate.TransformDirection(Vector3(0,0,something)); or you could just simply do crate.rigidbody.velocity = transform.forward * something; skovacs1 0 secs ago

Oct 21 '10 at 07:31 PM skovacs1

wow what a stupid mistake on my part. Thanks again!

Oct 21 '10 at 07:34 PM John 17
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1794
x244

asked: Oct 21 '10 at 06:57 PM

Seen: 3022 times

Last Updated: Oct 21 '10 at 06:57 PM