Why doesn't this work?

The idea is to instantiate a laser bolt inside each barrel, and have it fire out of the barrel. Here is the code:

function Fire()
{
	var go : GameObject;
	
	go = Instantiate(projectile);
	go.transform.parent = transform;
	go.transform.position = transform.position;
	go.transform.localRotation = transform.rotation;
	go.transform.parent = null;
	go.GetComponent("Projectile").SetDirection(Vector3.forward);
	
}

And below is the result. The laser bolt is not taking on the rotation of the gun barrel, it’s parent. Not sure it matters, but the gun barrels are children of the gun box, which rotates the barrels.

why do you need these 2 lines?

go.transform.parent = transform; 
...
go.transform.parent = null;

what is your script attached to?
If you print(transform.eulerAngles()) what do you get?

The problem is that you are killing it.

You set the rotation, just to kick it right again with the SetDirection in (0,0,1) (remember, vector3 forward is the forward of the 0 system, not of any specific object)

dreamora: SetDirection() doesn’t handle rotation of the object. It is a broken function that is supposed to set the movement direction vector (see below as to how I can’t figure out how to tell the bolt what it’s unity velocity should be).

The script is atteched to the barrels of the gun. The parent lines are relics of an effort to get the bolt to adopt the rotation of the barrels. I don’t need them. Anyway, that keyed me in on how to fix the rotation problem. But I have another problem now.

I don’t know how to get the bolts to move in the direction the barrels are pointing. even though the bolts may be oriented correctly, I can’t use Vector3.forward to move them in the direction they are pointing because they move along their z-axis, which hasn’t rotated. No matter how much the bolt rotates, the z-axis remains the same (the bolt rotates around the x-axis, so, theoretically, the Z-axis should be changing). So, the bolts are positioned and oriented correctly when instantiated inside the barrel, but the bolt’s aren’t moving in the correct direction.

While I’d be interested in the hard way of doing this (the way I was trying), I’ve discovered that creating an empty game object to use as a target point that is always positioned in front of the barrels works well enough. It also allows for tuning the gun convergence by adjusting the range of the object. Just killed 2 birds with one stone!!

try using transform.forward instead of Vector3.forward

great. nevermind my suggestion. didn’t see your last post.

Actually, the problem is pretty straightforward… You’re setting its rotation wrong.

go.transform.localRotation = transform.rotation; will make its rotation relative to the turret be equal to the rotation of the turret. So, it’d be like this:

Turret rotation: (30,0,30)
New object rotation (30,0,30) + (30,0,30);

Obviously that’d mess its rotation up. So, set it like this:

go.transform.rotation = transform.rotation;

Also, it won’t be the child of anything if you set go.transform.parent = null;

If you want it to rotate with its parent, go.transform.parent should be the object it should rotate with.

If that’s the object that created it, then go.transform.parent = transform;