- Home /
Create variable based on local axis
I am trying to instantiate 2 objects when one object gets destroyed. The problem is that if the rotation isn't straight up it gets instantiated at the wrong position.
var rot:Quaternion = transform.rotation;Problem is that the tempPos variables seem to be in world space. I need them to be local so it'll move the instantiated blox to the correct spot relative to the original object and it's rotation. I tried transform.Translate using Self space, but can't get it to work on the instantiated objects since it'll need to spawn in the correct world space then adjust to the correct relative local space from the now destroyed original object. Here's the question I suppose: How can I get a Vector3 variable to work with local and world space to get the instantiated object in the correct world position?
var targetBlock : GameObject;
tempPos2 = Vector3(transform.position.x,transform.position.y,transform.position.z+2);
tempPos3 = Vector3(transform.position.x,transform.position.y+2,transform.position.z);
var oneBlox : GameObject;
oneBlox = Instantiate (targetBlock, tempPos2, rot); oneBlox = Instantiate (targetBlock, tempPos3, rot);
Answer by rhys_vdw · Oct 06, 2012 at 04:25 PM
Use Transform.TransformPoint(). It will take a point in local co-ordinates, and translate it to world co-ordinates.
var rot : Quaternion = transform.rotation;
var targetBlock : GameObject;
tempPos2 = transform.TransformPoint(Vector3.forward * 2);
tempPos3 = transform.TransformPoint(Vector3.up * 2);
Instantiate (targetBlock, tempPos2, rot);
Instantiate (targetBlock, tempPos3, rot);
Very nice, thanks. I was hoping there was a built in way of doing it without needing sin and cos of the angles doing the whole trig thing.
Your answer
Follow this Question
Related Questions
Problem with bullet projectiles direction 2 Answers
Runtime bridge creator 0 Answers
overlapping object doesn't disable 1 Answer
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers
How to instantiate an object BEHIND my current position? 1 Answer