Unity3D 之3D游戏运动
Posted 命运的绯色结局
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D 之3D游戏运动相关的知识,希望对你有一定的参考价值。
3D运动,绑定了人形控制器后
using UnityEngine; using System.Collections; public class PlayerMove : MonoBehaviour { private CharacterController cc; private Animator animator; public float speed = 4; void Awake() { cc = this.GetComponent < CharacterController>(); animator = this.GetComponent<Animator>(); } // 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.1) { animator.SetBool("Walk", true); if (animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerRun")) { Vector3 targetDir = new Vector3(h, 0, v); transform.LookAt(targetDir + transform.position); cc.SimpleMove(transform.forward * speed); } } else { animator.SetBool("Walk", false); } } }
以上是关于Unity3D 之3D游戏运动的主要内容,如果未能解决你的问题,请参考以下文章
unity3d游戏开发脚本笔记之一:坐标系选择对物体运动的影响
[Unity3D]Unity3D游戏开发之从Unity3D到Eclipse