角色控制脚本Character.cs,为角色添加角色控制器Character controller。
<span style="font-size:14px;">using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour {
float attackInterval = 1.5f;
float timer = 0;
public float speed = 5.0F;
public float jumpSpeed = 10.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private Animator anim;
private CharacterController controller;
public Transform cameraTarget;
public GameObject effect1;
public GameObject effect2;
public GameObject effect3;
public GameObject AttackObj;
float Rot_y;
Health heroHealth;
Enemy enemy;
private bool isRun = false;
private bool isAttack = false;
private bool isDeath = false;
void Start () {
heroHealth = GetComponent<Health> ();
anim = GetComponent<Animator> ();
controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
if (heroHealth.currentheroHelth > 0) {
if (!isAttack) {
Move ();
}else
Attack ();
} else if(!isDeath){
anim.SetTrigger ("Death");
isDeath = true;
}
}
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Monster") {
isAttack = true;
anim.SetBool ("isAttack", true);
AttackObj = col.gameObject;
enemy = AttackObj.GetComponent<Enemy> ();
}
}
void OnTriggerExit(Collider col){
if (col.gameObject.tag == "Monster") {
isAttack = false;
anim.SetBool("isAttack",false);
AttackObj = null;
}
}
void Move(){
if (Input.GetKeyDown (KeyCode.LeftShift)) {
if (!isRun) {
isRun = true;
anim.SetBool ("isRun", true);
} else {
isRun = false;
anim.SetBool ("isRun", false);
}
}
if (controller.isGrounded) {
if (Input.GetButtonDown ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move( moveDirection * Time.deltaTime);
if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S)) {
anim.SetInteger (&