- Home /
How to add force or velocity to the rigid body?
using UnityEngine; using System.Collections;
public class PlayerShoot : Photon.MonoBehaviour {
public GameObject bulletPrefab;
public Transform muzzle;
// Use this for initialization
void Start ()
{
muzzle = transform.Find("ShootSpot");
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
SpawnMyMissle();
}
}
void SpawnMyMissle()
{
PhotonNetwork.Instantiate("MissleLaserMech", muzzle.position, muzzle.rotation, 0, null);
//gameObject("MissleLaserMech").rigidbody.AddForce(-transform.forward * 10000);
//The last argument is an optional group number, feel free to ignore it for now.
}
}
Answer by Tarlius · Feb 28, 2013 at 03:48 AM
Use RigidBody.AddForce and/or RigidBody.velocity. Read the docs for details. Note that AddForce is meant to be used in FixedUpdate, and velocity for one-offs.
Which is best will be a decision you'll have to make after trying both so see which feels best for your game.
is the main scripting look right.. i tried to add
rigidbody.velocity = Vector3(0,10,0);
and
rigidbody.AddForce (0, 10, 0);
I dont get any errors with rigidbody.AddForce, however it still does not shoot, it just sits there. i am a little stuck here...
and what do you think about the top portion.. the
muzzle = transform.Find("ShootSpot");
just does not work at all.. not sure what to do there...
You don't need the Find. Just transform will do.
Add force needs to be done in FixedUpdate. You need to use the new keyword in the first piece of code. That is probably the error you got.
rigidBody.velocity = new Vector3(0,10,0);
Note that unless you redefine rigidBody, it will default to the rigidBody on he GameObject you attached the script to. This might not be the behaviour you are after.
GameObject go = (GameObject) PhotonNetwork.Instantiate("$$anonymous$$issleLaser$$anonymous$$ech", muzzle.position, muzzle.rotation, 0, null);
RigidBody missleRigidBody = go.rigidBody;
missleRigidBody.velocity = new Vector3(0,10,0);
Also note that (0,10,0) will be directly up. I'm not sure if thats what you want? Also note that gravity will effect objects launched like this. You can disable gravity in the Physics settings, or you can make a script to move the object yourself.
If you have the mass at the default, 10 is a small force to add. Try 2000. $$anonymous$$ake sure the Is $$anonymous$$inematic flag is clear as well as all the constraints flags. You may also be hitting a collider in your scene. I don't know how your scene is setup, but Transform.Find() finds a child transform, so the "ShooterSpot" would have to be a child of whatever this script is attached to.
Ahh, you don't want the transform of the current object, do you? Remove the find and just drag it on in the inspector. $$anonymous$$uch easier and more efficient too :)
i tried that but it wont allow the drag and drop of the "muzzle" transform in the prefab... make sense?
Your answer
Follow this Question
Related Questions
Bullet Collision in Networking Game. 0 Answers
[Photon] Shoot Projectile multiplayer to mousePos 0 Answers
Photon shot collision 1 Answer
Photon Player Controls Other Player 0 Answers