using UnityEngine;
using System.Collections;
public
class
PlayerControl : MonoBehaviour
{
public
static
PlayerControl instance;
private
AnimationClip animationClip;
private
CharacterController controller;
public
float runSpeed =
5
;
private
float moveSpeed =
3
;
//走路速度
private
string currentState=
""
;
private
GameObject monster;
void
Awake()
{
instance =
this
;
}
void
Start ()
{
controller = GetComponent<CharacterController>();
print(transform.forward);
}
void
Update ()
{
KeyboardControl();
}
void
KeyboardControl()
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
//从自身坐标转换为世界坐标
if
(Input.GetKey(KeyCode.W))
{
if
(Input.GetKey(KeyCode.LeftShift))
{
currentState = RoleAnimationState.RUN;
//改变人物动画状态
PlayAnimation(currentState);
//播放动画
controller.SimpleMove(forward * runSpeed * Time.deltaTime*
100
);
}
else
{
currentState = RoleAnimationState.WALK;
PlayAnimation(currentState);
controller.SimpleMove(forward * moveSpeed * Time.deltaTime *
100
);
}
}
if
(Input.GetKey(KeyCode.S))
{
Vector3 back = transform.TransformDirection(Vector3.back);
currentState = RoleAnimationState.WALK;
PlayAnimation(currentState);
controller.SimpleMove(back * moveSpeed * Time.deltaTime *
100
);
}
if
(Input.GetKey(KeyCode.A))
{
Vector3 up = transform.TransformDirection(Vector3.up);
transform.Rotate(Vector3.up * -
50
* Time.deltaTime);
}
if
(Input.GetKey(KeyCode.D))
{
Vector3 up = transform.TransformDirection(Vector3.up);
transform.Rotate(Vector3.up *
50
* Time.deltaTime);
}
if
(Input.GetKey(KeyCode.J))
{
currentState = RoleAnimationState.ATTACK;
PlayAnimation(RoleAnimationState.ATTACK);
}
if
(Input.GetKey(KeyCode.K))
{
currentState = RoleAnimationState.ATTACK01;
PlayAnimation(RoleAnimationState.ATTACK01);
}
if
(currentState!=RoleAnimationState.IDLE && !animation.IsPlaying(currentState))
{
PlayAnimation(RoleAnimationState.IDLE);
}
}
void
PlayAnimation(string name)
{
currentState = name;
animationClip = animation[name].clip;
gameObject.animation.CrossFade(animationClip.name);
}
}