Projectile Malfunction

Ok, so here’s what happened. I’ve installed projectiles (bullets) to my game. However, there is a HUGE error. I tried to position my projectiles’ spawning point at my gun’s point. every time I enter the coordinates, it ends up somewhere else on the map, so I tried to do it manually, and I succeed at putting the multi-colored square at the gun’s point, but it fires at different points! Is this an editing or computer error? And if so, how can I fix it?

SCRIPT (Monodevelop)

var projectile : Rigidbody;
var speed = 200;

function Update () {

if (Input.GetButtonUp ("Fire1"))
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0,0,speed));

Destroy (clone.gameobject, 25);

First off, always assume that any problem is your issue, not a “computer error” or a bug in Unity. Of the 4200+ questions I’ve answered, only one was a bug in Unity. And assuming the problem is yours keeps you looking for a solution.

As for your code, you need to do the following:

  • Position your spawn point object at the end of the gun
  • Make your spawn point a child of the gun by dragging and dropping it gun in the Hierarchy
  • Rotate your spawn point object so that the ‘z’ axis (blue arrow in the Scene view) is aligned with the barrel of the gun.

Your code looks fine. The only other potential issue is if the clone is hitting the gun or some other object as it is being fired. If so, you can use Physics.IgnoreCollision() to fix the problem.