- Home /
Question by
Brian Foster · Apr 22, 2011 at 03:19 AM ·
shoot
shooting somthig
Hi, can you please tell me the steps of shooting a gun in Unity 3D? I really need HELP!!!!
Comment
Answer by robertmathew · Apr 22, 2011 at 03:25 AM
var projectile : Rigidbody; private var speed = 5;
function Update() { if(Input.GetButtonDown("Fire1")) { var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity =transform.TransformDirection(Vector3(speed, 0, 0));
}
Give your object that you are firing a rigidbody (then turn off gravity)
Answer by Kashaunzilla · Jun 15, 2011 at 01:10 AM
This is very simple to do. Try this and it will shoot what you want to shoot.
//place your name of shooting target here. In inspector the projectile has to have a rigidbody
var gameObject = Rigidbody;
function Update ()
{
//When you the left mouse button fires the gameObject
if ( Input.GetButtonDown (Fire1) )
{
//the shooting part of the function. Create empty gameObject place under your player and name spawnPoint. Then place this script on top of main player gameObject
var gameObject : Instantiate(gameObject, transform.Find(spawnPoints).transform.position, Quaternion.identity);
//adding force to projectile
Rigidbody.AddForce : gameObject(forward * 500);
}
}
This script is very easy to make and use.
Your answer