Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CWinstead · Oct 10, 2017 at 11:14 PM · movementmovement scriptnoobswitchcase

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CWinstead · Oct 11, 2017 at 12:26 AM

That did it! Thank you!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stop player from moving 1 Answer

How do I fix this movement issue? 1 Answer

Slowly move a GameObject on 1 axis, then destroy it. 1 Answer

Updated Question if statement not executing case 1 Answer

I use switch in if...it desn't work 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges