Last active
August 29, 2015 14:19
-
-
Save mosluce/3412937b2551c6058649 to your computer and use it in GitHub Desktop.
Unity Practice~ Demo Video: https://www.youtube.com/watch?v=yp4EgIM5uiM&feature=youtu.be
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 CharacterController ctrl; | |
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"); | |
} | |
} | |
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); | |
var v = moveVector * 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