- Home /
How do i instantiate along the z axis?
So my empty object that i have creates 10 wall segments 10 units apart... How do i make the created segments follow the z axis with the same rotation as the creator so that any rotation i put on to the creator, the wall segments will always follow the z axis in a straight line?
Basically if i rotate the creator and press a button, 10 wall segments will be created 10 units apart in a straight line any way i rotate the creator along the local z axis?
This is what i have so far, but i cant seem to make i follow the z axis in a local straight line... Ive tried a crazy amount of things and been stuck for awhile. Thanks for the help!
#pragma strict
//point B Marker
var target: Transform;
public var Wall: Transform;
var create = false;
function Start () {
}
function Update () {
if(Input.GetKeyUp("space")){
create = true;
}
transform.LookAt(target);
var relativePos = target.position - transform.position;
var Distance = Vector3.Distance(target.position, transform.position);
var ModelsNeeded = Distance / 10;
var Fwd = transform.TransformDirection(Vector3.forward);
print("Distance to other: " + Distance);
print("Models Needed for Distance: " + ModelsNeeded);
if(create == true){
for (var i : int = 0;i < ModelsNeeded; i++) {
Instantiate (Wall, Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z + (i * 10)), Quaternion.LookRotation(relativePos));
create = false;
}
}
}
Answer by robertbu · Jun 28, 2013 at 12:37 AM
I'm assuming you want the local 'z' of the object, not the world 'z' axis. It will always be transform.forward. To calculate the position you want:
var v3Pos = transform.position + transform.forward * (i * 10);
Instantiate (Wall, v3Pos, transform.rotation);
Thank you so much man! I had var in my script that had transform.forward i just didnt know how to set it up correctly. you just solved a 3 day issue of $$anonymous$$e.. as sad as that sounds. Thanks ALOT lol
Answer by mohitramani · Jun 27, 2013 at 10:35 AM
I didnt understand your question completely but I guess the following should help you.
lets say you have a prefab named Cube and a clone of that cube is placed in the scene which will be your creator to which you can apply rotation(using some script of yours) and then press some key to create N clones behind it.
public GameObject prefab;
void Start () {
}
void Update () {
if(Input.GetKey("SomeKey"))
{
for(int i=0;i<N;i++)
{
Instantiate(prefab,new Vector3(transform.position.x,transform.position.y,transform.position.z + (i*10)),transform.rotation);
}
}
}
You didn't change a thing... except the rotation at the end and i have already tried that as well. And why did you delete some stuff? I don't understand your answer.
Im not trying to rotate my creator, but rather create wall segments down the z axis. Say you have the z axis right.. when i press space i want to create wall segments down the z-axis between 2 point in 3d space.
say
point A is at x= 0, y=0 , z=0,
point b is at x= 10, y=0, z=10
I want to create wall segments down a straight diagonal line that from point A TO point B that all have the save local rotation.
Bump! Please help ive been stuck for a couple of days now thanks
You never actually said what the problem was, but I think I can guess. You're instantiating the object using the localPosition, but the instantiation occurs in global space, so it's likely that everything is working, just in the wrong place.
Exactly! I probably explaned it wrong. I need all the wall segments to be locals of the creator that run down along with the z-axis of the creator in a straight line!