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()
{

}
}

时间: 2024-08-30 10:15:07

u3d Animator和脚本控制FPS骑士的相关文章

U3D——Unity3D的脚本-script入门

 Unity3D的基本操作很容易就能掌握了,接下来就是游戏系统的核心部分:脚本. 什么是Script(脚本)?简而言之,就是使用代码来执行一系列动作命令的特殊文本,它需要编译器来从新解读.U3D内部如何解读脚本,这不是我们所要关心的-这是引擎开发人员的活,我们所要知道的就是脚本的使用规则. [三种语言的特点] U3D支持C#,JavaScript,BOO三种语言格式的代码编写.首先来简单介绍下这三种语言的特点: 对U3D来说,这是入门级的脚本语言,U3D内置的函数都能通过JS方便的调用.语法

Unity3D中的第三人称镜头的脚本控制

原地址:http://blog.csdn.net/mobanchengshuang/article/details/27591271 好久没有敲Blog了,谢谢大家的留言.关注.私信等支持,但是我好像已经没有办法让自己继续写以前的博客系列了,因为我发现网上关于unity3D的内容太少了,所以我无法自拔地想写U3D相关的文章!!! 第三人称视角 第三人称视角是什么?很简单,CS就是一种第一人称视角游戏,玩家没有办法看到自己的角色形象,只能观察除开自己之外的游戏内容.第三人称视角那么就明显是能够看到

【HTML5】用脚本控制交互元素details元素的使用

1.源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Con

Unity3D 学习教程 11 c#脚本控制摄像头

首先新建一个脚本 点击创建一个文件夹起名C# 点击文件夹 创建一个C#脚本 建好文件后 双击文件 启动脚本编辑器 void Start () 是场景运行时加载程序 void Update ()  是每调用一针执行一次  可以认为是试试执行的程序 下面编写第一个脚本 控制摄像机移动 using UnityEngine; using System.Collections; public class acc : MonoBehaviour { int speed=50; void Start () {

用脚本控制虚拟机

#############用脚本控制虚拟机 给file.sh 一个权限 chmod +x file.sh

Unity3D教程宝典之光影烘焙:第四讲脚本控制

上一讲讲了用Light probes实现动态物体的非实时阴影,这一讲讲用代码实现代码实现动态物体的非实时阴影. 实现步骤:(1)新建一个场景,并建一个plane作为大地(2)创建Cube并缩放成扁平面后.复制Cube并旋转拼接搭建成一个敞篷.(3)讲上述物体设置static并烘焙.(4)创建一个player.这里用的unity自带的Character Controller包里的 3rd person controller这个prefab,拖进敞篷边.在该人物下找到Bip001 Pelvis这个节

AudioMixer的脚本控制

AudioMixer是Unity5新特性之一,能很好的实现立体声效果. 这儿先记录一下脚本控制的方法: 1.添加一个Group,然后点击它 2.右侧面板上出现2个参数:pitch(速度)和volume(大小) 3.右键Pitch,点击Expose to script 4.回到AudioMixer窗口,点击其右上角的Exposed Parameters,下拉有一个参数,双击修改为X 5.编写脚本: Using UnityEngine.Audio; public AudioMixer mixer;

脚本控制animation的事件

由于动作设计经常修改动作,所以每次改完都要再添加一次animation的事件,所以就直接写了个脚本,当然以后可以做成表格,然后用脚本从表格中读取,然后生成对应的animation事件.在Assets/Editor目录中放置代码,参考代码如下: using UnityEngine; using System.Collections; using UnityEditor; public class AddEventsToAnimations : MonoBehaviour { [MenuItem("

c#脚本控制shader

如图所示,c#脚本控制shader颜色. 1 public class ControlColor : MonoBehaviour 2 { 3 public Color color = new Color(1, 1, 1, 1); 4 public Material mat; 5 public void ChangeColor() 6 { 7 mat.SetVector("_Diffuse", color); 8 } 9 } 1 [CustomEditor(typeof(ControlC