How would I instantiate an object so that it is right in front of the object that is instantiating it? In other words, so that it is slightly offset from the parent object’s z axis. Is there a way to just add something about the z axis to the “transform.position” part of the common instantiate code?
Thanks for any ideas.
I think its something like
vector3+(0,0,0.5)
or
transform.Position+0,0,0.5
fiddle with the brackets etc
AC
something like transform.position + transform.forward * 42.0 (42 here just being some distance, use whatever you like)
Thanks guys. I’ll try these out as soon as I can.
Hmm. I wasn’t able to get those working after all. I played around with them and then some stuff I found in the docs on instantiating but nothing I tried was accepted by Unity… It seems like this should be pretty managable - does anyone know what I am missing?
It would probably be useful to see the code you used.
Sorry, my mistake. I should have kept a record of what I tried but I didn’t. I went back to try stuff over again so I could post the code and found that this:
Instantiate(Obj, transform.position + transform.forward * 1.2, transform.rotation);
does work. I am not sure what I muddled before, but hopefully I have learned to be more patient and to keep notes!
Thanks guys!
This is a similar problem that I have, and wondered how I can get it to work correctly.
function FixedUpdate()
{
//Using Powerups
if(Input.GetButtonDown("Powerup") hasBomb == true)
{
var bomb = Instantiate(bombPrefab, GameObject.Find("shootPoint1").transform.position, Quaternion.identity);
bomb.rigidbody.velocity = Vector3(0,10,15);
hasBomb = false;
}
}
So this is a one off powerup, and it seems to work pretty well except that “shootPoint1” is an empty game object on my player…which serves at its spawn point, and this prefab is spawning relative to the world, not to that point.
The bomb is always going in said Vector3, rather than lobbing from my player.
It’s hard to say what exactly is going wrong without knowing more about ‘shootPoint1’ (what its transform is, where it is in the hierarchy, etc.). I will go ahead and mention though that you generally don’t want to check input ‘down’ and ‘up’ functions in FixedUpdate(), as it’s possible to miss events that way (keep in mind that Update() is called every frame, but FixedUpdate() may not be called every frame).
Makes sense in the FixedUpdate sense. Changed that.
shootPoint1 is the spawning point for the Prefab, and it is a child of the Player GameObject.
Assuming it will always be in the same direction,
var tmpPos = transform.position;
tmpPos.z += 1.2;
Instantiate(Obj, tmpPos, transform.rotation);
[code]
You could also add extra stuff for different directions by doing if statements and changing it from tmpPos.z+=1.2, to tmpPos.z-=1.2 or tmpPos.x+=1.2, or tmpPos.x-=1.2;
I did this earlier today, so I know it works. I did .y+=2 because I needed to make particles appear roughly 2 above where the object was located.
Hope this helps.
Yes, it’s the parent transform.
I how does that code make the instantiated prefab launch from its relative position instead of world position?
Because it’s getting the relative position from the gameobject creating it.
var tmpPos = transform.position; ← This line. Since we are adding to transform.position’s values, it’s relative to the object that is doing the instantiating.
I believe what you are looking for is TransformPoint()
var theDistance=2;
var thePosition = transform.TransformPoint(Vector3.forward * theDistance);
Instantiate(object, thePosition, parent.transform.rotation);