!!!!!bullet fires only on the left never straight!!!!!!!

Hy im a little developer(age 13) and i have a problem wile copying a script in youtube unity tutorials the wepons keeps fireing in the left. i tried to turning the spawn point
and it didn’t help. Here my script:

var Bullet : Transform;
var Spawn : Transform;
var BulletSpeed = 100;

(java)
function Update () 
{
   if(Input.GetButtonDown("Fire1")){
       Fire();
    }

}

function Fire()
{
   var Bullit = Instantiate(Bullet,Spawn.position,Spawn.rotation);
   
   Bullit.rigidbody.AddForce(transform.forward * BulletSpeed);    
}

In saw that if we add the “*BulletSpeed);” part it shoots in the left but if i don’t put it,it shoots straight but false down directly.

Try to use the 5th button to format your code or your can highlight them then press Ctrl+K. Try: bullet.rigidbody.AddForce(bullet.forward* bulletSpeed); It is possible that the gameObject you attached this script to is facing left, so when you add force using the gameObject orientation, it will always fly left from your view.

If you don't need the bullet to be affected by gravitation, you can set it's RigidBody-component's isKinematic to 'true'.

1 Answer

1

You’re spawning the bullet at the rotation of Spawn.rotation. Check three things:

  1. Make sure the Spawn transform matches the rotation of the gun or thing shooting the bullet.
  2. Make sure the Spawn transform is facing FORWARDS. I think your object is simply facing the left.
  3. Also fiddle around with your bullet rigidbody add force. If your bullet prefab is skewed in rotation, then when it shoots it’ll be skewed as well.

I’m aware this question is more than a year old and you’ve probably found the answer somewhere else by now but in case anyone else has this question I hope this helps.