- Home /
How to set up a compass for 2d game?
I've been trying to figure out a compass that points to 8 directions, North, East, South, West, North East, North West, South East, South West. It's a top-down game based game using the arrow keys as inputs for moving and changing directions. I'm using c# and well I've got it pretty much done except when stopping on an angle direction(north east, south east, south west, north west). The character will not stay facing that way instead it will go to one of the two.
Example if I move my character by pressing up arrow key and right arrow key North East is the direction I'm facing, but when released it will go to East or North I guess which ever key I release last the direction will face.
I'm just looking for guidance on how to fix this issue, as well if you see any flaws in coding.
here is my code(don't mind the crappy coding):`
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float speed= 200f; // movememnt speed.
public int dir=2; // direction character is faceing. 1= north, 2 = south, 3 = west, 4 = east, 5 = north west, 6 = north east, 7 = south west, 8 = south east.
public bool move; // if character is moving
float stimer=1f; // used to set timer back to 1.
public float timer = 1f; // timer
public bool timetrig=false;// trigger the timer
void Update(){
if (timetrig == true) {
timer -= Time.deltaTime;
}
if (timer <= 0.0f) {
timer = stimer;
timetrig=false;
}
// timer^
if( Input.GetKey (KeyCode.UpArrow) && timetrig==false){
move= true;
dir=1;
} else if (dir == 1 && move == true) {
move = false;
}
if (Input.GetKey(KeyCode.DownArrow) && timetrig == false) {
move = true;
dir = 2;
} else if (dir==2 && move == true) {
move = false;
}
if (Input.GetKey (KeyCode.LeftArrow) && timetrig == false) {
move = true;
dir = 3;
} else if (dir==3 && move == true) {
move = false;
}
if (Input.GetKey (KeyCode.RightArrow) && timetrig == false) {
move = true;
dir = 4;
} else if (dir==4 && move == true) {
move = false;
}
if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)&& timetrig == false) {
dir = 5;
} else if (dir==5 && Input.GetKeyUp (KeyCode.LeftArrow) || Input.GetKeyUp (KeyCode.UpArrow)) {
timetrig=true;
}
if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.UpArrow)&& timetrig == false ) {
dir = 6;
} else if (dir==6 && Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.UpArrow) ) {
timetrig=true;
}
if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.DownArrow)&& timetrig == false) {
dir = 7;
} else if (dir==7 && Input.GetKeyUp (KeyCode.LeftArrow) || Input.GetKeyUp (KeyCode.DownArrow)) {
timetrig=true;
}
if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)&& timetrig == false) {
dir = 8;
} else if (dir==8 && Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.DownArrow)) {
timetrig=true;
}
}
// trying to set up the timer with all the directions, didn't work to well.
void FixedUpdate () {
if (Input.GetKey (KeyCode.LeftArrow) && timetrig == false) {
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.RightArrow) && timetrig == false) {
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.UpArrow) && timetrig == false) {
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.DownArrow) && timetrig == false) {
transform.position += Vector3.down * speed * Time.deltaTime;
}
// moving the character.^
}
}`
I didn't proof your code carefully to check for errors, but once I saw the gist I figure your assumption is accurate. However it may be the case that you'll snap to a Cartesian direction even if you release your keys in the same frame because of how you're structuring your input logic.
Is there a great reason you can't use Input.GetAxis to get a horizontal / vertical vector pair? That's considered the proper way to handle what you're doing. It may also present an opportunity to solve this problem, because you can do anything to those vectors before making use of them, such as ensuring it's possible to "come to rest" on a NW, NE, SW, or SE vector.
It'll be a lot less code, a lot less messy, and would help you in the long run, I promise.
Well I got two reasons for not using the getAxis method.
Later on I want the person playing the game to be able to change the keys to use the character, and from what I read online I can't change the keys for the getAxis vectors while in gameplay.
I tried it with the getAxis method and still the same out come. I'll definitely make an effort to using it on more time though.
Nine times out of ten, when you find yourself writing a hundred lines of conditional checks with very-similar statements to execute, there's a much, much better way. Think algorithmically! ;)
The come-to-rest-on-diagonal issue is something you'll have to address directly. I'm not sure how "best" to do this given your approach, because of the way each successive statement is executed. I believe your logic guarantees you will never stop while facing along a diagonal.
If you manipulate your input prior to making use of it, you could create a condition which allows co$$anonymous$$g to rest on a diagonal.
Answer by NoseKills · Mar 13, 2015 at 02:00 PM
Something like this might be simpler. Seems to be possible to release the keys simultaneously so the direction is left diagonal, but with someReleased
and anyPressed
you could set a delay on changing direction when releasing keys (maybe a delay 1 Update() would be enough) because it doesn't always happen.
public float speed= 200f; // movememnt speed.
bool anyPressed;
bool someReleased;
bool up;
bool down;
bool left;
bool right;
Vector3 direction = new Vector3 ();
void Update()
{
someReleased =
Input.GetKeyUp (KeyCode.UpArrow) ||
Input.GetKeyUp (KeyCode.DownArrow) ||
Input.GetKeyUp (KeyCode.LeftArrow) ||
Input.GetKeyUp (KeyCode.RightArrow);
up = Input.GetKey(KeyCode.UpArrow);
down = Input.GetKey(KeyCode.DownArrow);
left = Input.GetKey(KeyCode.LeftArrow);
right = Input.GetKey(KeyCode.RightArrow);
anyPressed = up || down || left || right;
if (anyPressed)
{
direction = Vector3.zero;
if (up)
direction.y += 1;
if (down)
direction.y -= 1;
if (right)
direction.x += 1;
if (left)
direction.x -= 1;
}
}
void FixedUpdate ()
{
if (anyPressed)
{
transform.position += direction * speed * Time.deltaTime;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
2D Top Down Character Control 2 Answers