u3d Animator和脚本控制FPS骑士

Posted 0null0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
{

}
}

以上是关于u3d Animator和脚本控制FPS骑士的主要内容,如果未能解决你的问题,请参考以下文章

U3D帧动画的制作

U3D 一个简单的角色控制脚本

[小巩u3d] Stats窗口

C#代码,运用button控制键盘。

Unity—ParticleSystem(粒子系统)与Animator(动画状态机)批量管理器

Unity动画系统Animator动态添加事件