Created
June 3, 2015 13:08
-
-
Save smallrice45/c15f471ba21dfd6a253d to your computer and use it in GitHub Desktop.
簡易2D角色控制
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class KeyboardEventListener : MonoBehaviour { | |
public delegate void KeyboardHandler(int dir); | |
public event KeyboardHandler onHorizontalMove; | |
public event KeyboardHandler onVerticalMove; | |
private KeyCode m_LeftArrowKey = KeyCode.LeftArrow; | |
private KeyCode m_RightArrowKey = KeyCode.RightArrow; | |
private KeyCode m_UpArrowKey = KeyCode.UpArrow; | |
private KeyCode m_DownArrowKey = KeyCode.DownArrow; | |
void Update () { | |
// Press Right | |
if (Input.GetKeyDown (m_RightArrowKey)){ | |
OnHorizontalMove(1); | |
} | |
if (Input.GetKeyUp (m_RightArrowKey)){ | |
if (Input.GetKey (m_LeftArrowKey)){ | |
OnHorizontalMove(-1); | |
}else{ | |
OnHorizontalMove(0); | |
} | |
} | |
// PressLeft | |
if (Input.GetKeyDown (m_LeftArrowKey)){ | |
if (Input.GetKey (m_RightArrowKey)){ | |
OnHorizontalMove(1); | |
}else{ | |
OnHorizontalMove(-1); | |
} | |
} | |
if (Input.GetKeyUp (m_LeftArrowKey)){ | |
if (Input.GetKey (m_RightArrowKey)){ | |
OnHorizontalMove(1); | |
}else{ | |
OnHorizontalMove(0); | |
} | |
} | |
// Press Up | |
if (Input.GetKeyDown (m_UpArrowKey)){ | |
OnVerticalMove(1); | |
} | |
if (Input.GetKeyUp (m_UpArrowKey)){ | |
if (Input.GetKey (m_DownArrowKey)){ | |
OnVerticalMove(-1); | |
}else{ | |
OnVerticalMove(0); | |
} | |
} | |
// PressDown | |
if (Input.GetKeyDown (m_DownArrowKey)){ | |
if (Input.GetKey (m_UpArrowKey)){ | |
OnVerticalMove(1); | |
}else{ | |
OnVerticalMove(-1); | |
} | |
} | |
if (Input.GetKeyUp (m_DownArrowKey)){ | |
if (Input.GetKey (m_UpArrowKey)){ | |
OnVerticalMove(1); | |
}else{ | |
OnVerticalMove(0); | |
} | |
} | |
} | |
void OnHorizontalMove(int dir) | |
{ | |
if (onHorizontalMove != null){ | |
onHorizontalMove (dir); | |
} | |
} | |
void OnVerticalMove(int dir) | |
{ | |
if (onVerticalMove != null){ | |
onVerticalMove (dir); | |
} | |
} | |
static public KeyboardEventListener Get (GameObject go) | |
{ | |
KeyboardEventListener listener = go.GetComponent<KeyboardEventListener>(); | |
if (listener == null) listener = go.AddComponent<KeyboardEventListener>(); | |
return listener; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class PlayerInput : MonoBehaviour { | |
private KeyboardEventListener m_KeyboardListener; | |
private PlayerMovement m_PlayerMovement; | |
void Awake(){ | |
m_PlayerMovement = GetComponent<PlayerMovement>(); | |
ListenerKeyboard(); | |
} | |
void ListenerKeyboard(){ | |
m_KeyboardListener = GetComponent<KeyboardEventListener>(); | |
if (m_KeyboardListener != null && m_PlayerMovement != null){ | |
m_KeyboardListener.GetComponent<KeyboardEventListener>().onHorizontalMove += CharacterHorizontalMovement; | |
m_KeyboardListener.GetComponent<KeyboardEventListener>().onVerticalMove += CharacterVerticalMovement; | |
} | |
} | |
void CharacterHorizontalMovement(int theDir){ | |
m_PlayerMovement.moveDir.x = (float)theDir; | |
m_PlayerMovement.SetDir(theDir); | |
} | |
void CharacterVerticalMovement(int theDir){ | |
m_PlayerMovement.moveDir.y = (float)theDir; | |
m_PlayerMovement.SetDir(theDir); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class PlayerMovement : MonoBehaviour { | |
public float moveSpeed = 1.0f; | |
public Vector2 moveDir; | |
public bool faceRight = true; | |
// Update is called once per frame | |
void Update () { | |
this.GetComponent<Rigidbody2D>().velocity = moveDir * moveSpeed*20/1 * Time.deltaTime; | |
} | |
public void SetDir(float theDir){ | |
if (theDir > 0 && !faceRight){ | |
FlipDir(); | |
}else if (theDir < 0 && faceRight){ | |
FlipDir(); | |
} | |
} | |
void FlipDir(){ | |
faceRight = !faceRight; | |
this.transform.localScale = new Vector3(-this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment