Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 LazySamurai · Mar 12, 2015 at 02:42 PM · c#2d character2d controller

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.^
                 

 

             
     }

}`

Comment
Add comment · Show 3
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 AlwaysSunny · Mar 12, 2015 at 02:42 AM 1
Share

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.

avatar image LazySamurai · Mar 12, 2015 at 02:55 AM 0
Share

Well I got two reasons for not using the getAxis method.

  1. 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.

  2. 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.

avatar image AlwaysSunny · Mar 12, 2015 at 05:39 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
         }
     }


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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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