Unity_角色控制详解
来源:互联网 发布:域名污染 gfw 编辑:程序博客网 时间:2024/06/11 23:02
Unity_角色控制详解
1、CharacterController 角色控制器
最简单最常用的的方式。
using UnityEngine;using System.Collections;public class PlayerMove : MonoBehaviour { private CharacterController cc; public float speed = 10; private Animator animator; void Awake() { cc = this.GetComponent<CharacterController>(); animator = this.GetComponent<Animator>(); }// Use this for initializationvoid Start () {}// Update is called once per frame void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); if (Joystick.h != 0 || Joystick.v != 0) { h = Joystick.h; v = Joystick.v; } if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f) { animator.SetBool("walk", true); if (animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerRun")) { Vector3 targetDir = new Vector3(-v, 0, h); transform.LookAt(transform.position + targetDir); cc.SimpleMove(targetDir * speed); } } else { animator.SetBool("walk", false); } }}
using UnityEngine;using System.Collections; [RequireComponent(typeof(CharacterController))]public class ExampleClass : MonoBehaviour { public float speed = 3.0F; public float rotateSpeed = 3.0F; void Update() { CharacterController controller = GetComponent<CharacterController>(); transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0); Vector3 forward = transform.TransformDirection(Vector3.forward); float curSpeed = speed * Input.GetAxis("Vertical"); controller.SimpleMove(forward * curSpeed); }}
2、Rigidbody 刚体
刚体是另一种控制方式。 操作时注意可以锁定刚体x/y/z/轴的角度旋转,和y轴的position。
下面使用一个官方的demo:
void FixedUpdate(){float moveHorizontal = Input.GetAxis("Horizontal");float moveVertical = Input.GetAxis("Vertical");Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);GetComponent<Rigidbody>().velocity = movement;}
3、动画控制。
动画往往是这样的:比如一个walk的动画。当执行这个动画时,我们的角色就会向前walk,从而产生位移。所以通过控制动画的播放。可以达到控制角色的目的。
动画状态机。如下:
Demo:
using UnityEngine;using System.Collections;public class Player : MonoBehaviour { public float moveSpeed = 4; public Animator anim;// Use this for initializationvoid Awake () { anim = this.GetComponent<Animator>();}// Update is called once per framevoid Update () { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); if (Input.GetKey(KeyCode.LeftShift)) { anim.SetBool("Sneak", true); } else { anim.SetBool("Sneak", false); } if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1) { float newSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 5.6f, moveSpeed * Time.deltaTime); anim.SetFloat("Speed", newSpeed); Vector3 targetDir = new Vector3(h,0,v); Quaternion newRotation = Quaternion.LookRotation(targetDir, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, 10); } else { float newSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 0, moveSpeed * Time.deltaTime*10); anim.SetFloat("Speed", newSpeed); } if(anim.GetCurrentAnimatorStateInfo(0).IsName("locomotion")){ PlayFootMusic(); } else { StopFootMusic(); } } void PlayFootMusic() { if (!audio.isPlaying) { audio.Play(); } } void StopFootMusic() { if (audio.isPlaying) { audio.Stop(); } }}
以上代码则带到了目的。其中关键代码就是:
float newSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 5.6f, moveSpeed * Time.deltaTime); anim.SetFloat("Speed", newSpeed); //设置Speed为newSpeed,来控制状态机 //以下代码达到转向的目的 Vector3 targetDir = new Vector3(h,0,v); Quaternion newRotation = Quaternion.LookRotation(targetDir, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, 10);
0 0
- Unity_角色控制详解
- thinkphp基于角色的权限控制详解
- thinkphp基于角色的权限控制详解
- Spring Security 3 基于角色访问控制过程详解
- ThinkPHP的RBAC(基于角色权限控制)详解
- ThinkPHP的RBAC(基于角色权限控制)详解
- Spring Security 3 基于角色访问控制过程详解 .
- ThinkPHP的RBAC(基于角色权限控制)详解
- ThinkPHP的RBAC(基于角色权限控制)详解
- Spring Security 3 基于角色访问控制过程详解
- ThinkPHP的RBAC(基于角色权限控制)详解
- ThinkPHP的RBAC(基于角色权限控制)详解
- 角色访问控制RBAC
- Ogre角色控制
- 角色访问控制(RBAC)
- 基于角色权限控制
- Ogre角色控制
- 鼠标控制角色
- R语言抓取pm2.5数据绘制全国pm2.5分布图
- IO流笔记01
- Linux学习笔记:系统启动引导过程
- leetcode 130: Surrounded Regions
- ListView的工作原理及优化
- Unity_角色控制详解
- Oracle清理回收站的方法
- TYVJ 1330 乳草的入侵
- /sys/class/gpio 文件接口操作IO端口(s3c2440)
- CSS3属性box-flex
- hdoj5253
- OC_Notifications(通知)
- IO流笔记02
- 从零开始学习iOS开发-股票记帐本1.0(1)