The question is answered, right answer was accepted
[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!
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.
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.
Split this in to two and place it in the comments. It doesnt go in the Answer box!
Follow this Question
Related Questions
Making an App Prototype without Coding 0 Answers
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
Setting perspective camera origin 0 Answers
Respawn point 1 Answer