- Home /
World Map Help
So I'm trying to do something where you press left/right to move the cursor to the next location waypoint. Here's what I have:
using UnityEngine;
using System.Collections;
public class WorldMap : MonoBehaviour {
public GameObject[] MapLocations;
int MapLocationIndex;
public GameObject MapCursor;
// Use this for initialization
void Start () {
MapLocationIndex = 0;
}
// Update is called once per frame
void Update () {
if (MapLocationIndex >=5)
{
MapLocationIndex = 5;
}
if (MapLocationIndex <=0)
{
MapLocationIndex = 0;
}
print (MapLocationIndex);
MapCursor.transform.position = MapLocations [MapLocationIndex].transform.position;
if (Input.GetAxis ("Horizontal") > 0)
{
MapLocationIndex++;
}
if (Input.GetAxis ("Horizontal") < 0)
{
MapLocationIndex--;
}
}
}
While I can move between the locations by pressing left/right, what's going on is I immediately go from Location 1 to Location 6 by having the key pressed for less than a second. How can I fix this so it stops at each location instead of just going through it if the key is held?
Answer by robertbu · Oct 05, 2014 at 08:46 PM
GetAxis() returns true every frame while the 'Horizontal" axes button or devices is pressed. You needs something that returns true for on the single frame where the button goes down. Input.GetButtonDown() for example, or Input.GetKeyDown().
if (Input.GetKeyDown(KeyCode.RightArrow)) {
MapLocationIndex++;
}
The problem with that is I want the game to be controller compatible as well as work with input changes, which is why I didn't use Get$$anonymous$$eyDown. I tried GetButtonDown("Horizontal Positive Button") but it would just give me an error saying Horizontal Positive had not been assigned yet.
Specifically, the error I get with GetButtonDown("Horizontal Positive Button") is "UnityException: Input Button Horizontal Positive Button is not setup. To change the input settings use: Edit -> Project Settings -> Input"
Your answer
