velocity issue on pushing an object forward.

I am making a object throwing script and have an issue:

“velocity” is not a member of UnityEngine.GameObject

Here is my script.

Grenade.velocity = transform.TransformDirection( Vector3( 0, 0, ThrowPower) );

1 Answer

1

Grenade is a GameObject and velocity has to be accessed through the rigidbody of the gameobject.
Make sure your grenade object has a rigidbody component attached and then use this instead:

Grenade.rigidbody.velocity = transform.TransformDirection( Vector3( 0, 0, ThrowPower) );

I no longer get an error, but the force doesn't take play. Yes the grenade has a rigidbody, and yes I have tried raising the force. It doesn't move a bit. Is the line of code not good for pushing objects?

You should probably rather try something like this using [AddRelativeForce][1]: Grenade.rigidbody.AddRelativeForce (Vector3.forward * 10); [1]: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddRelativeForce.html

Still no luck. Anything I could be missing on the grenade? The rigid body is at its default settings.

Could you post the whole script so I can have a quick look? Can't see anything wrong with the code, should work fine if the grenade has a rigidbody attached.. Although you will of course want to test some different values to make sure there isn't just too little force. For example try changing the 10 to a 100 etc: Grenade.rigidbody.AddRelativeForce (Vector3.forward * 100);

Ok try replacing your grenadefunction() with this: function grenadefunction() { var ThrowGrenade : GameObject = Instantiate(Grenade, transform.position, Quaternion.identity); ThrowGrenade.rigidbody.AddRelativeForce (Vector3.forward * ThrowPower); }