- Home /
Instantiate Path
Hi
I'm currently trying to make a temple run style game. I have written a script which allow me to instantiate a number of cubes in a row, I've called this my "Path builder" i was wondering how do i change the direction in which the row is created.
function makePath() { for (var x = 0; x < brickLength; x++) { nextPosition += (cube.renderer.bounds.size.x); yPosition = (cube.renderer.bounds.size.y);
var cube = Instantiate(cube, Vector3 ( nextPosition,(yPosition)*0.5 ,0 ), transform.rotation);
cube.name = "cube"+x;
}
}
every thing i try seems to just change the actually instantiated object...
Answer by aldonaletto · May 13, 2012 at 03:43 PM
A simple way to control the direction is to use a unit vector dir showing the direction - you can assign Vector3.forward to go in this object's forward direction, Vector3.right to the right Vector3.left to the left, etc. This dir vector is rotated according to the current transform rotation, thus it's actually relative to the object to which this script is attached.
var dir: Vector3 = Vector3.right;
function makePath() { dir = transform.TransformDirection(dir); // rotate dir to the current transform orientation var nextPos = Vector3.zero; // calculate positions in nextPos nextPos.y = cube.renderer.bounds.size.y 0.5; for (var x = 0; x < brickLength; x++) { nextPos.x += dir.x cube.renderer.bounds.size.x; nextPos.z += dir.z * cube.renderer.bounds.size.z; var cube = Instantiate(cube, nextPos, transform.rotation); cube.name = "cube"+x; } }
I get:
Assets/Sam Folder/pathFiles/scripts/buildPath.js(55,44): BCE0022: Cannot convert 'float' to 'UnityEngine.Vector3'.
i think its related to:
nextPos = cube.renderer.bounds.size.y * 0.5;
Do you know why that would happen? I've had to change some of your script to make it get this far:
var dir: Vector3;
function Update() {
(im planning on making a trace, so that depending on weather your input is left or right the dir = left or right. if any one could point me in the right direction for that as well, it'd be greatly appreciated)
dir = Vector3.right; makePath();
}
function makePath() { dir = transform.TransformDirection(dir); // rotate dir to the current transform orientation var nextPos = Vector3(0,0,0); // calculate positions in nextPos nextPos = cube.renderer.bounds.size.y 0.5; for (var x = 0; x < brickLength; x++) { nextPos.x += dir.x cube.renderer.bounds.size.x; nextPos.z += dir.z * cube.renderer.bounds.size.z; var cube = Instantiate(cube, nextPos, transform.rotation); cube.name = "cube"+x; } }
$$anonymous$$y bad! I forgot a ".y" in nextPos! Answer fixed.