How to adjust Instantiate position with local coordinate?

var adjustPosition : Vector3;
Instantiate (launcherPrefab, barrel.position+ adjustPosition, barrel.rotation);

In above script, I want Prefab instantiated slightly in front of [barrel.position].
How to?

If I set (adjustPosition)'s z value to something like 2 or 3, it represent world-coordinate, so that if character turns(barrel’s position is also turns), Instantiate position will not be match my purpose.

It does not generated in front of, but side of.

Try Vector3,Forward to get local z forward direction.

Try this easy setup. Place an empty game object as a child of the barrel where you want instantiation to occur. Then just use that objects transform to instantiate.

HTH
BTH

Thanks for reply.

I tried like,

var V3forward : float;
barrel.position+ adjustPosition+Vector3.forward * V3forward

and changing V3forward value ±, but it still looks world-coordinate.

It’s transform.forward for local. Vector3.forward is world coordinates.

You can also use

var barrelPosition : Vector3;

function Shoot() {
  var shootFromPosition : Vector3 = transform.TransformPoint( barrelPosition );
}

Ummm… duh… like he said. transform.forward. I prefer to use aim-and-shoot empty gameObjects objects myself and just instantiate at that position and rotation.

But…why would Vector3.Forward always be world coordinates. Makes it not very useful. Not got Unity in front of me but it seems if you use the Vector3 as a property of the object that is the local. With no object it is world.

BTH

It’s often useful to have the world axes easily available. For example, you might want to raycast straight down from an arbitrarily oriented object, or make an arbitrarily oriented object ‘jump’ by applying an impulse in the ‘up’ direction.

Don’t get too excited !