脚本用法:
先给你的player绑定一个CharacterController组件
按住shift可加速跑动,Tab键盘切换战斗状态
由于很多动作都没有,例如向左跑,向右跑,这里用向前跑来代替了
- using UnityEngine;
- using System.Collections;
-
- public class PlayerMovement : MonoBehaviour {
- //是否战斗姿态
- public bool isAttackStand =false;
- //是否可以移动;
- public bool steering = true;
-
- public void PlayerAllowMove(bool steer){
- steering =steer;
- }
- //行走初始速度;
- public float walkSpeed = 20.0f;
- //跑的速度;
- public float runSpeed = 60.0f;
- //是否在行走;
- public bool isWalking = true;
- //是否在攻击;
- public bool isAttacking = false;
-
- public void setAttacking(bool isAttack){
- isAttacking = isAttack;
- if(isAttack){
- isAttackStand = true;
- }
- }
- //是否在地上;
- private bool grounded = true;
- //移动速度;
- private float moveSpeed = 0.0f;
- //行走的方向;
- private Vector3 moveDirection= Vector3.zero;
- //重力
- public float gravity = 50.0f;
- //获取人物动画组件;
- Animation playerAnimation;
- //
- private CharacterController controller;
-
- public enum MovementState{
- Walk,
- WalkForward,
- WalkLeftSlide,
- WalkRightSlide,
- WalkBack,
- WalkLeft,
- WalkRight,
-
- Run,
- RunForward,
- RunLeftSlide,
- RunRightSlide,
- RunBack,
- RunLeft,
- RunRight,
-
- AttackStand,
- Idle
- }
- private MovementState movementState = MovementState.Idle;
-
-
-
- void Awake(){
- //程序开启前,获取人物动画组件;
- playerAnimation = GetComponent <Animation>() as Animation;
- //获取人物的CharacterController组件;
- controller = GetComponent<CharacterController>() as CharacterController;
- }
-
- // Use this for initialization
- void Start () {
- playerAnimation["Idle"].layer = -1;
- playerAnimation["AttackStandy"].layer = -1;
- playerAnimation["Walk"].layer = -1;
- playerAnimation["Run"].layer = -1;
- }
-
- // Update is called once per frame
- void Update () {
- if(steering){
- if(grounded){
- if(isAttackStand==false){
- movementState = MovementState.Idle;
- }else{
- movementState = MovementState.AttackStand;
- }
- if(Input.GetAxis("Vertical")!=0||Input.GetAxis("Horizontal")!=0){
- Vector3 MoveForward = transform.TransformDirection(Vector3.forward);
- if(isWalking){
- //走路动作
- if(Input.GetAxis("Vertical") > 0){
- movementState = MovementState.WalkForward;
- }else if(Input.GetAxis("Vertical") < 0){
- movementState = MovementState.WalkBack;
- }
-
- if(Input.GetAxis("Horizontal")>0){
- if(Input.GetAxis("Vertical") > 0){
- movementState = MovementState.WalkRightSlide;
- }else{
- movementState = MovementState.WalkRight;
- }
- }else if(Input.GetAxis("Horizontal") < 0){
- if(Input.GetAxis("Vertical") > 0){
- movementState = MovementState.WalkLeftSlide;
- }else{
- movementState = MovementState.WalkLeft;
- }
- }
- }else{
- //跑动动作
- if(Input.GetAxis("Vertical") > 0){
- movementState = MovementState.RunForward;
- }else if(Input.GetAxis("Vertical") < 0){
- movementState = MovementState.RunBack;
- }
-
- if(Input.GetAxis("Horizontal")>0){
- if(Input.GetAxis("Vertical") > 0){
- movementState = MovementState.RunRightSlide;
- }else{
- movementState = MovementState.RunRight;
- }
- }else if(Input.GetAxis("Horizontal") < 0){
- if(Input.GetAxis("Vertical") > 0){
- movementState = MovementState.RunLeftSlide;
- }else{
- movementState = MovementState.RunLeft;
- }
- }
- }
- }
- }
- movementLogic();
- GetSpeed();
- moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= moveSpeed;
- moveDirection.y -= gravity * Time.deltaTime;
-
- var flags = controller.Move(moveDirection * Time.deltaTime);
- grounded = (flags & CollisionFlags.Below) != 0;
- //切换是否战斗状态;
- if(Input.GetKeyUp(KeyCode.Tab)){
- isAttackStand = !isAttackStand;
- }
- //按下左shift键切换跑步;
- if(Input.GetKeyDown(KeyCode.LeftShift)){
- isWalking = false;
- }
- //弹起左shift切换为走路;
- if(Input.GetKeyUp(KeyCode.LeftShift)){
- isWalking = true;
- }
- }
- //永远面向摄像机方向;
- transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
- }
-
- void movementLogic(){
- if (movementState == MovementState.Idle){
- playerAnimation.CrossFade("Idle", 0.15f);
- }else if (movementState == MovementState.AttackStand){
- playerAnimation.CrossFade("AttackStandy", 0.15f);
- }else if (movementState == MovementState.WalkForward){
- playerAnimation.CrossFade("Walk", 0.15f);
- //控制前进播放;
- playerAnimation["Walk"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
- }else if (movementState == MovementState.WalkBack){
- playerAnimation.CrossFade("Walk", 0.15f);
- //控制后退播放;
- playerAnimation["Walk"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
- }else if (movementState == MovementState.WalkLeft){
- playerAnimation.CrossFade("Walk", 0.15f);
- }else if (movementState == MovementState.WalkRight){
- playerAnimation.CrossFade("Walk", 0.15f);
- }else if (movementState == MovementState.WalkLeftSlide){
- playerAnimation.CrossFade("Walk", 0.15f);
- }else if (movementState == MovementState.WalkRightSlide){
- playerAnimation.CrossFade("Walk", 0.15f);
- }else if (movementState == MovementState.RunForward){
- playerAnimation.CrossFade("Run", 0.15f);
- //控制前进播放;
- playerAnimation["Run"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
- }else if (movementState == MovementState.RunBack){
- playerAnimation.CrossFade("Run", 0.15f);
- //控制后退播放;
- playerAnimation["Run"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
- }else if (movementState == MovementState.RunLeft){
- playerAnimation.CrossFade("Run", 0.15f);
- }else if (movementState == MovementState.RunRight){
- playerAnimation.CrossFade("Run", 0.15f);
- }else if (movementState == MovementState.RunLeftSlide){
- playerAnimation.CrossFade("Run", 0.15f);
- }else if (movementState == MovementState.RunRightSlide){
- playerAnimation.CrossFade("Run", 0.15f);
- }
- }
-
- float GetSpeed () {
- if (movementState == MovementState.Idle){
- moveSpeed = 0;
- }else if(movementState == MovementState.WalkForward){
- moveSpeed = walkSpeed;
- }else if(movementState == MovementState.WalkBack||movementState == MovementState.WalkRight||movementState == MovementState.WalkRightSlide||movementState == MovementState.WalkLeft||movementState == MovementState.WalkLeftSlide){
- moveSpeed = walkSpeed*0.95f;
- }else if(movementState == MovementState.RunForward){
- moveSpeed = runSpeed;
- }else if(movementState == MovementState.RunBack||movementState == MovementState.RunRight||movementState == MovementState.RunRightSlide||movementState == MovementState.RunLeft||movementState == MovementState.RunLeftSlide){
- moveSpeed = runSpeed*0.95f;
- }
- if(isAttacking==true){
- moveSpeed = 1.0f;
- }
- return moveSpeed;
- }
- }
复制代码
|
|