Instantiation problem

Hi, I have this script that is supposed to shoot out a shell:

var shellVelocity = Vector3 (0,0,0);
var shellToSpawn : Rigidbody;
// Must be same as GunBasis
var fireRate : float = 0.15;
private var canEject : boolean = true;

function Update() {
if (GunBasis.isShooting  canEject) {
	Shell();
}
}

function Shell() {
	canEject = false;
	var shell : Rigidbody;	
	shell = Instantiate (shellToSpawn, transform.position, transform.rotation);
		shell.velocity = shellVelocity;
		yield WaitForSeconds (fireRate);
		canEject = true;
}

I had it first in the gun script itself, but gave the same problem.
Rather that instantiating it at the same place as the object it is attached to, it always is instantiating at one point, never where the object currently is. Any ideas what this could be caused by? :?

I think it has to do because the transform.rotation of the gun is still, and the camera is the one that has rotation. How would I access the transform component of the camera to base this off of? :?

you could place the gun inside the camera so that it moves with it or you can simply add to your script

var cam : Camera;

And then assign the camera you want to the script in the editor

then change

shell = Instantiate (shellToSpawn, transform.position, transform.rotation);

to

shell = Instantiate (shellToSpawn, cam.transform.position, cam.transform.rotation);

I have a code that is pretty similar, and the only difference is I have a line of code to ignore collision with the collider on the gun, I have the velocity set up differently, this is how your code looks with those added. I added in the Ignore collision and changed the velocity so the bullet actually moves.

var shellVelocity = Vector3(200,0,0); 
var shellToSpawn : Rigidbody; 
// Must be same as GunBasis 
var fireRate : float = 0.15; 
private var canEject : boolean = true; 

function Update() { 
if (GunBasis.isShooting  canEject) { 
   Shell(); 
} 
} 

function Shell() { 
   canEject = false; 
   var shell : Rigidbody;    
   shell = Instantiate (shellToSpawn, transform.position, transform.rotation); 
Physics.IgnoreCollision(shell.collider, transform.root.collider);
      shell.velocity = shellVelocity; 
      yield WaitForSeconds (fireRate); 
      canEject = true; 
}

None work. Even with the gun on the default main camera, some thing…

Try putting the script on an empty object and put the empty object inside your gun

That seams to work, but it looks like it is in world velocity. Like, if I am rotated at 0 y, it works. but rotate 180 degrees it goes in the opposite direction

Yes, it is in world velocity. I set it so the instantiated shell is a child of the eject point, but still is world. Do you know how to convert the object’s world velocity to local velocity?

FIXED IT! I had to put instad of:shell.velocity = shellVelocity;

shell.velocity = transform.TransformDirection(shellVelocity);

it works now! YEA! :smile: