- Home /
Shooting script not working properly
I'm using this script to shoot:
var bulletPrefab:Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
//Create bullet
var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
//Direction of bullet and add force
bullet.rigidbody.AddForce(transform.forward * 5000);
}
}
But it does not shoot properly. When I turn right it does not shoot, it just creates the bullet in front of the gun. Also when I shoot at certain angles it does not shoot to where I am aiming it shoots to the left. Besides this it works fine. What am I doing wrong?
Answer by Seth-Bergman · Jul 23, 2012 at 08:19 PM
You need to change "Quaternion.identity", which aligns the rotation to the world axis, to "transform.rotation" or "spawnPoint.transform.rotation"
and maybe change "transform.forward" to "bullet.transform.forward"
Answer by Ingen · Jul 23, 2012 at 08:20 PM
hi!
the spawnPoint is a bit forward the gun and is a child of maincamera?
Answer by Hassam1957234 · Jan 19, 2013 at 05:23 PM
Hi i was using this script var bulletPrefab:Transform;
function Update () {
if(Input.GetButtonDown("Fire1"))
{
//Create bullet
var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, transform.rotation);
//Direction of bullet and add force bullet.rigidbody.AddForce(bullet.transform.forward * 5000);
} } first of all i cant drag a bullet prefab to inspector of my rifle and it wont shoot
Your answer
Follow this Question
Related Questions
Shooting & Animation Problem 1 Answer
not losing ammo when shooting 1 Answer
Problems with a shooting script 1 Answer
How to make a gun in unity? 1 Answer
1 script doesn't react to input 1 Answer