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 /
  • Help Room /
This question was closed Nov 24, 2015 at 07:46 AM by Crixu for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Crixu · Nov 23, 2015 at 12:56 PM · unity 5newbie

[Newby] Need help with navigating between a previous and next position

I want to setup a Mario like map system (see link)

Currently I use this code to see where the player can go.

 nextButton = currentButton.GetComponent<LevelButton> ().next;
         previousButton = currentButton.GetComponent<LevelButton> ().next;
 
         if (nextButton.transform.position.x > this.transform.position.x) {
         canMoveRight = true;
     } else if (nextButton.transform.position.x < this.transform.position.x) {
         canMoveLeft = true;
     } else if (nextButton.transform.position.y > this.transform.position.y) {
         canMoveUp = true;
     } else if (nextButton.transform.position.y < this.transform.position.y) {
         canMoveDown = true;
     } 
 
         if (previousButton.transform.position.x > this.transform.position.x) {
             canMoveRight = true;
         } else if (previousButton.transform.position.x < this.transform.position.x) {
             canMoveLeft = true;
         } else if (previousButton.transform.position.y > this.transform.position.y) {
             canMoveUp = true;
         } else if (previousButton.transform.position.y < this.transform.position.y) {
             canMoveDown = true;
         }

Any noob can see that using this only one direction will be set to true. Also, the booleans will stay on true.

How can I fix this? Or perhaps even optimize this code?

Any help is appreciated!

Comment
Add comment · Show 1
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 savlon · Nov 23, 2015 at 01:04 PM 0
Share

If i were to implement something similar to what you've shown in the link, I would be using a waypoint system... There is no reason your player should have a free roam capability if what you're implementing is the same as your link right? A waypoint system will help ensure your player doesn't magically glitch out and walk to places he/she shouldn't. There are plenty tutorials for a waypoint system, just use google.

1 Reply

  • Sort: 
avatar image
0

Answer by Crixu · Nov 23, 2015 at 02:05 PM

That's what the booleans are for, here's the complete script:

 using UnityEngine;
 using System.Collections;
 
 public class LevelSelectController : MonoBehaviour {
 
     public bool canMoveLeft;
     public bool canMoveRight;
     public bool canMoveUp;
     public bool canMoveDown;
 
     public bool canMove;
 
     public GameObject previousButton;
     public GameObject nextButton;
 
     public GameObject currentButton;
 
     public float speed = 5.0f;
 
     // Use this for initialization
     void Start () {
         this.transform.position = GameObject.Find ("LevelButton").transform.position;
         currentButton = GameObject.Find ("LevelButton");
 
         canMove = true;
     }
     
     // Update is called once per frame
     void Update () {
         nextButton = currentButton.GetComponent<LevelButton> ().next;
         previousButton = currentButton.GetComponent<LevelButton> ().next;
 
         if (nextButton.transform.position.x > this.transform.position.x) {
             canMoveRight = true;
         } else if (nextButton.transform.position.x < this.transform.position.x) {
             canMoveLeft = true;
         } else if (nextButton.transform.position.y > this.transform.position.y) {
             canMoveUp = true;
         } else if (nextButton.transform.position.y < this.transform.position.y) {
             canMoveDown = true;
         } 
 
         if (previousButton.transform.position.x > this.transform.position.x) {
             canMoveRight = true;
         } else if (previousButton.transform.position.x < this.transform.position.x) {
             canMoveLeft = true;
         } else if (previousButton.transform.position.y > this.transform.position.y) {
             canMoveUp = true;
         } else if (previousButton.transform.position.y < this.transform.position.y) {
             canMoveDown = true;
         }
 
         if (Input.GetKeyDown (KeyCode.A) && canMoveLeft && canMove) {
             StartCoroutine(Move());
         }
 
         if (Input.GetKeyDown (KeyCode.D) && canMoveRight && canMove) {
             StartCoroutine(Move());
         }
 
         if (Input.GetKeyDown (KeyCode.W) && canMoveUp && canMove) {
             StartCoroutine(Move());
         }
 
         if (Input.GetKeyDown (KeyCode.S) && canMoveDown && canMove) {
             StartCoroutine(Move());
         }
 
         if (Input.GetKeyDown (KeyCode.E)) {
             Application.LoadLevel(currentButton.GetComponent<LevelButton>().level);
         }
     }
 
     IEnumerator Move(){
         canMove = false;
 
         GameObject next = currentButton.GetComponent<LevelButton> ().next;
         GameObject prev = currentButton.GetComponent<LevelButton> ().prev;
 
         if (currentButton.GetComponent<LevelButton> ().next == nextButton) {
             this.transform.position = next.transform.position;
 //            transform.position = Vector3.MoveTowards(transform.position, next.transform.position, speed * Time.deltaTime);
             currentButton = next;
 
             if(transform.position == next.transform.position){
                 canMove = true;
             }
         } else if (currentButton.GetComponent<LevelButton> ().prev == previousButton) {
             this.transform.position = prev.transform.position;
 //            transform.position = Vector3.MoveTowards(transform.position, prev.transform.position, speed * Time.deltaTime);
             currentButton = prev;
 
             if(transform.position == prev.transform.position){
                 canMove = true;
             }
         }
 
         yield break;
     }
 }
 

Do note it's not fully done yet, this is about 15-20 minutes of work.

The currentButton is a object the player is on, like the level squares on the Mario map.

Comment
Add comment · Show 2 · 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 meat5000 ♦ · Nov 23, 2015 at 03:40 PM 0
Share

Split this in to two and place it in the comments. It doesnt go in the Answer box!

avatar image Crixu meat5000 ♦ · Nov 23, 2015 at 03:50 PM 0
Share

Apologies, didn't know about this.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Making an App Prototype without Coding 0 Answers

how do I rezolve "look rotation viewing vector is zero"? 1 Answer

Trying to get a simple bit of GUIText to fade in and out using transparency. Wrote a script that should only affect one GUIText but all of them are fading in and out. What am I doing wrong? 1 Answer

Setting perspective camera origin 0 Answers

Respawn point 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