Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Created August 3, 2015 17:22
Show Gist options
  • Save smallrice45/1e642d4a01d3c640cc9d to your computer and use it in GitHub Desktop.
Save smallrice45/1e642d4a01d3c640cc9d to your computer and use it in GitHub Desktop.
頭戴裝置FPS角色控制邏輯。
using UnityEngine;
using System.Collections;
public class FirstPersonCamera : MonoBehaviour {
public Transform m_Player;
public Transform m_Target;
public float direction;
// Update is called once per frame
void Update () {
FollowTarget ();
CalAngle();
}
void FollowTarget(){
Vector3 position = m_Target.position;
transform.position = position;
}
void CalAngle(){
Vector3 rootDirection = this.transform.forward;
rootDirection.y = 0;
Vector3 moveDirection = m_Player.transform.forward;
moveDirection.y = 0;
Vector3 axis = Vector3.Cross(rootDirection, moveDirection);
direction = Vector3.Angle(rootDirection, moveDirection) / (axis.y > 0 ? -1 : 1);
}
}
using UnityEngine;
using System.Collections;
public class KeyboardEventListener : MonoBehaviour {
public delegate void KeyboardHandler(int dir);
public event KeyboardHandler onHorizontalKey;
public event KeyboardHandler onVerticalKey;
private KeyCode m_LeftArrowKey = KeyCode.LeftArrow;
private KeyCode m_RightArrowKey = KeyCode.RightArrow;
private KeyCode m_UpArrowKey = KeyCode.UpArrow;
private KeyCode m_DownArrowKey = KeyCode.DownArrow;
static public KeyboardEventListener Get (GameObject go){
KeyboardEventListener listener = go.GetComponent<KeyboardEventListener>();
if (listener == null) listener = go.AddComponent<KeyboardEventListener>();
return listener;
}
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 (onHorizontalKey != null){
onHorizontalKey (dir);
}
}
void OnVerticalMove(int dir)
{
if (onVerticalKey != null){
onVerticalKey (dir);
}
}
}
using UnityEngine;
using System.Collections;
public class PlayerInput : MonoBehaviour {
public enum GameInputType { Keyboard, Gamepad};
public GameInputType m_InputType;
public delegate void InputHandler(float dir);
public event InputHandler onHorizontalInput;
public event InputHandler onVerticalInput;
void Awake(){
switch (m_InputType){
case GameInputType.Keyboard:
ListenerKeyboard();
break;
case GameInputType.Gamepad:
break;
default:
ListenerKeyboard();
break;
}
}
// 監聽鍵盤事件,分為水平與垂直
void ListenerKeyboard(){
KeyboardEventListener.Get(gameObject).onHorizontalKey += OnHorizontalKey;
KeyboardEventListener.Get(gameObject).onVerticalKey += OnVerticalKey;
}
void OnHorizontalKey(int theDir){
if (onHorizontalInput != null){
onHorizontalInput((float)theDir);
}
}
void OnVerticalKey(int theDir){
if (onVerticalInput != null){
onVerticalInput((float)theDir);
}
}
static public PlayerInput Get (GameObject go){
PlayerInput PlayerInput = go.GetComponent<PlayerInput>();
if (PlayerInput == null) PlayerInput = go.AddComponent<PlayerInput>();
return PlayerInput;
}
}
using UnityEngine;
using System.Collections;
public class PlayerMovement3D : MonoBehaviour {
private Animator m_Animator;
private Vector2 moveDir;
private bool isRush;
public Transform viewTarget;
public FirstPersonCamera m_FPSCamera;
private AnimatorStateInfo m_AnimatorState;
// Use this for initialization
void Awake () {
InitGetCompoment();
ListenerInput();
}
void InitGetCompoment(){
m_Animator = GetComponent<Animator>();
}
// Update is called once per frame
void ListenerInput () {
PlayerInput.Get(gameObject).onHorizontalInput += OnGetHorizontalMovementInput;
PlayerInput.Get(gameObject).onVerticalInput += OnGetVerticalMovementInput;
PlayerInput.Get(gameObject).onHorizontalInput += OnSetAnimatorSpeed;
PlayerInput.Get(gameObject).onVerticalInput += OnSetAnimatorSpeed;
}
void OnGetHorizontalMovementInput(float dir){
m_Animator.SetFloat("Horizontal", dir);
moveDir.x = dir;
}
void OnGetVerticalMovementInput(float dir){
m_Animator.SetFloat("Vertical", dir);
moveDir.y = dir;
}
void OnSetAnimatorSpeed(float dir){
// GetMoveSpeedVector
float moveSpeed = Vector2.SqrMagnitude(moveDir.normalized);
// RushSpeed
if (isRush){
moveSpeed *= 2;
}
m_Animator.SetFloat( "Speed", moveSpeed);
}
void OnAnimatorIK(int layerIndex)
{
m_AnimatorState = m_Animator.GetCurrentAnimatorStateInfo(0);
bool inMove = m_AnimatorState.IsTag("LocoMotion");
if (viewTarget){
m_Animator.SetLookAtPosition(viewTarget.position);
m_Animator.SetLookAtWeight(1.0f);
}
// Set FaceDir to Fix Turn
if (m_FPSCamera){
m_Animator.SetFloat("FaceDir", m_FPSCamera.direction);
}
// WhenMove Rotate Character
if (inMove){
transform.eulerAngles += new Vector3(0,m_FPSCamera.direction*Time.deltaTime*10,0) ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment