u3d Animator和脚本控制FPS骑士
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
Transform _head;
Animator _animator;
void Start () {
_head = transform.FindChild("Head");
_animator = GetComponent<Animator>();
}
void Update () {
Move();
Rotate();
Run();
Jump();
}
public float moveSpeed =10f;
void Move()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
bool isWalking = horizontal != 0 || vertical != 0;
_animator.SetBool("Walk", isWalking);
bool isBacking = Input.GetKey(KeyCode.S);
_animator.SetBool("Back", isBacking);
bool isLefting = horizontal < 0;
_animator.SetBool("Left",isLefting);
bool isRighting = horizontal > 0;
_animator.SetBool("Right",isRighting);
bool walkFight = (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))&&(isWalking || isLefting || isRighting || isBacking);
_animator.SetBool("WalkFight", walkFight);
if (walkFight)
{
if (Input.GetMouseButtonDown(0) )
{
_animator.SetTrigger("WalkSwingLeft");
}
if (Input.GetMouseButtonDown(1))
{
_animator.SetTrigger("WalkSwingRight");
}
if (Input.GetMouseButtonDown(2))
{
_animator.SetTrigger("WalkTrust");
}
}
if (horizontal==0&&vertical==0)
{
_animator.SetBool("Walk",false);
if (Input.GetMouseButtonDown(0))
{
_animator.SetTrigger("SwingLeft");
}
if (Input.GetMouseButtonDown(1))
{
_animator.SetTrigger("SwingRight");
}
if (Input.GetMouseButtonDown(2))
{
_animator.SetTrigger("Trust");
}
}
Vector3 desPos = (transform.right * horizontal + transform.forward * vertical) * Time.deltaTime * moveSpeed;
transform.position += desPos;
}
public float rotateSpeed = 1f;
void Rotate()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
transform.Rotate(Vector3.up*mouseX*rotateSpeed);
_head.Rotate(-Vector3.right * mouseY * rotateSpeed);
}
public void Run()
{
bool isRuning = Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift);
_animator.SetBool("Run", isRuning);
if (isRuning)
{
moveSpeed = 6f;
}
else
{
moveSpeed = 3f;
}
}
public void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_animator.SetTrigger("Jump");
}
}
void WalkSwing()
{
}
}