Created
April 25, 2015 13:12
-
-
Save mosluce/f53a827953448a00adbe to your computer and use it in GitHub Desktop.
This file contains hidden or 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 UnityChanController : MonoBehaviour | |
{ | |
public float gravity = 9.8f; | |
public float jumpSpeed = 1.0f; | |
public CharacterController ctrl; | |
float currentJumpSpeed = 0.0f; | |
Animator animator; | |
// Use this for initialization | |
void Start () | |
{ | |
animator = GetComponent<Animator> (); | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
//移動控制 | |
MoveControl (); | |
JumpControl (); | |
} | |
void OnCallChangeFace () | |
{ | |
} | |
void JumpControl () | |
{ | |
if (Input.GetButtonDown("Jump")) { | |
if(animator.GetCurrentAnimatorStateInfo(0).IsName("Standing@loop")) animator.SetInteger("JumpState", 0); | |
else if(animator.GetCurrentAnimatorStateInfo(0).IsName("Walking@loop")) animator.SetInteger("JumpState", 1); | |
else if(animator.GetCurrentAnimatorStateInfo(0).IsName("Running@loop")) animator.SetInteger("JumpState", 2); | |
animator.SetTrigger("Jump"); | |
currentJumpSpeed = jumpSpeed; | |
} | |
} | |
void MoveControl () | |
{ | |
//動畫參數(非實際速度) | |
var speed = animator.GetFloat ("Speed"); | |
//垂直操縱稈推移量 | |
var movev = Input.GetAxis ("Vertical"); | |
//水平操縱稈推移量 | |
var moveh = Input.GetAxis ("Horizontal"); | |
//移動向量 | |
var moveVector = new Vector3 (moveh, 0.0f, movev); | |
//動畫控制 | |
if (moveVector != Vector3.zero) { | |
if (speed <= 3.0f) | |
speed += Time.deltaTime; | |
else | |
speed = 3.0f; | |
} else { | |
speed = 0.0f; | |
} | |
animator.SetFloat ("Speed", speed); | |
currentJumpSpeed -= gravity * Time.deltaTime; | |
if (ctrl.isGrounded && currentJumpSpeed<0.0f) | |
currentJumpSpeed = 0.0F; | |
var v = (moveVector + new Vector3(0, currentJumpSpeed, 0)) * Time.deltaTime; | |
//偵測動畫狀態 | |
if(animator.GetCurrentAnimatorStateInfo (0).IsName ("Running@loop")) v *= 2; | |
//控制移動 | |
ctrl.Move (v); | |
//方向控制 | |
if (moveVector != Vector3.zero) { | |
transform.rotation = Quaternion.LookRotation (moveVector); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment