- Home /
Creating characters that can move on their own. Problems adding Z Axis to movement.
Everything works great on the X and Y but im getting no movement on the Z axis. Im sure im overlooking something I just cant spot it. Help!
public class MoveBox : MonoBehaviour { //Variables int speed; //creates a speed variable Vector3 dir; //this control the direction string myDir; // this will float maxX, maxY, minX, minY, minZ, maxZ; //boundaries for the cube
// Use this for initialization
void Start () {
//choose a random speed.
speed = Random.Range(8,16);
ChooseBarrier ();
ChooseDir();
}
void ChooseBarrier(){
//sets the Max and Min Values
maxX = Random.Range(6,16); // sets Max X to between 10 and 16
minX = -maxX; // this sets the min X value to be the negative of the Max X value
maxY = maxX; // this sets the Max Y value to be the same as the Max X
minY = minX;// this sets the Min Y to be the same as the Min X
minZ = minX;
maxZ = maxX;
}
void ChooseDir(){ // choose the direction -- call a choose diretion function
ChooseBarrier ();
// create a temporary int to determine direction
int whichWay = Random.Range (0, 4); // 0 will be right, 1 will be down, 2 will be left, 3 will be up, 4 will be forward, 5 will be backward
switch (whichWay) {
case 0: // if which way equals 0
dir = Vector3.right;
myDir = "right";
break; //ends the action
case 1: //if which way equals 1
dir = Vector3.down;
myDir = "down";
break; //ends the action
case 2: //if which way equals 2
dir = Vector3.left;
myDir = "left";
break; //ends the action
case 3: //if which way equals 3
dir = Vector3.up;
myDir = "up";
break; //ends the action
case 4:
dir = Vector3.forward;
myDir = "forward";
break; //ends the action
case 5:
dir = Vector3.back;
myDir = "backward";
break; //ends the action
}
}
void Move(Vector3 direction){
transform.Translate (direction * speed * Time.deltaTime); // this will make the character move at a speed and direction
}
void checkPos (string direction){ // this will check the direction and determine if there is a barrier
switch (direction) {
case "right": // if {direction == Vector3.down}
if (transform.position.x > maxX) {
ChooseDir ();
}
break;
case "down":
if (transform.position.y < minY) {
ChooseDir ();
}
break;
case "left":
if (transform.position.x < minX) {
ChooseDir ();
}
break;
case "up":
if (transform.position.y > maxY) {
ChooseDir ();
}
break;
case "forward":
if (transform.position.z > maxZ) {
ChooseDir ();
}
break;
case "backward":
if (transform.position.z < minZ) {
ChooseDir ();
}
break;
}
}
// Update is called once per frame
void Update () {
Move (dir); // calls the move function using the dir variable to determine direction
checkPos(myDir); // check
}
}
Comment
Best Answer
Answer by orethoion · Oct 11, 2017 at 12:03 AM
This is probably because your Random.Range (0, 4) can only return 0, 1, 2, or 3. Random.Range returns an integer between the range that is inclusive for the first integer and exclusive for the second integer.
To fix this, just use Random.Range(0, 6).