unity3d之如何控制人物移动、旋转和动画播放
代码源自噩梦射手,记录一下方便后续使用,顺便将老师的解释给备注上去_(:з」∠)_
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput; namespace CompleteProject
{
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f; // The speed that the player will move at. Vector3 movement; // The vector to store the direction of the player's movement.
Animator anim; // Reference to the animator component.
Rigidbody playerRigidbody; // Reference to the player's rigidbody.
#if !MOBILE_INPUT
int floorMask; // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
float camRayLength = 100f; // The length of the ray from the camera into the scene.
#endif void Awake ()
{
#if !MOBILE_INPUT
// Create a layer mask for the floor layer.
floorMask = LayerMask.GetMask ("Floor");
#endif // Set up references.
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
} void FixedUpdate ()
{
// Store the input axes.
float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
float v = CrossPlatformInputManager.GetAxisRaw("Vertical"); // Move the player around the scene.移动物体
Move (h, v); // Turn the player to face the mouse cursor.转动物体
Turning (); // Animate the player.动画物体
Animating (h, v);
} void Move (float h, float v)
{
// Set the movement vector based on the axis input.
movement.Set (h, 0f, v); // Normalise the movement vector and make it proportional to the speed per second.
//归一化,为了保持在不同cpu上,每秒走的速度是一样的
movement = movement.normalized * speed * Time.deltaTime; // Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);
} void Turning ()
{
#if !MOBILE_INPUT
// Create a ray from the mouse cursor on screen in the direction of the camera.
//知道这一帧鼠标点在哪里
//怎么从鼠标的屏幕空间点直接得到射线
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition); // Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit; // Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
//得到一条从玩家到鼠标的有方向的射线
Vector3 playerToMouse = floorHit.point - transform.position; // Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f; // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
// 得到一个playerToMouse射线的四元数(四元数是啥可以百度一下)
Quaternion newRotatation = Quaternion.LookRotation (playerToMouse); // Set the player's rotation to this new rotation.
//就可以将人物转向新的方向了
playerRigidbody.MoveRotation (newRotatation);
}
#else Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Mouse X") , 0f , CrossPlatformInputManager.GetAxisRaw("Mouse Y")); if (turnDir != Vector3.zero)
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = (transform.position + turnDir) - transform.position; // Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f; // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotatation = Quaternion.LookRotation(playerToMouse); // Set the player's rotation to this new rotation.
playerRigidbody.MoveRotation(newRotatation);
}
#endif
} void Animating (float h, float v)
{
// Create a boolean that is true if either of the input axes is non-zero.
// 创建一个布尔值判断键盘是否有输入值
bool walking = h != 0f || v != 0f; // Tell the animator whether or not the player is walking.
//告诉animator人物是否在走动,walking为true时,玩家从静止动画到走动动画,为false反之
anim.SetBool ("IsWalking", walking);
}
}
}
玩家动画状态机设置
创建一个Animator Controller








unity3d之如何控制人物移动、旋转和动画播放的更多相关文章
- [Unity3D]Unity3D游戏开发之使用EasyTouch虚拟摇杆控制人物移动
大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei.今天呢,我们来一起学习在Unity3D中使用EasyTouch虚拟摇杆来控制人物移动.虽然Un ...
- (转)在Unity3D中控制动画播放
用Unity3D也算是好久了,但是每次做项目总还是能学到新的东西.这次做一个TPS的项目就遇到了这样一个问题,如何同时在上下半身播放不同的动画?解决方法其实是很简单,但由于对于动画资源的了解不足导致问 ...
- Unity学习笔记_控制人物移动+摄像机跟随
我想做的移动操作方式类似[流星蝴蝶剑].[龙之谷].[我的世界第三人称]的第三人称操作方式. 操作说明:W键会朝当前镜头方向前进,鼠标控制镜头旋转. 做前需知(先去稍微了解一下比较好): ①unity ...
- iOS 控制单个控制器旋转
iOS 控制单个控制器旋转 控制单个ViewController 的旋转 //不旋转,保持竖屏 //iOS 5 - (BOOL) shouldAutorotateToInterfaceOrientat ...
- 可控制转速CSS3旋转风车特效
以前制作网页动画一般使用javascript,现在已经有越来越多动动画使用纯CSS实现,并且动画的控制也可以使用CSS3实现,因为CSS 3来了,CSS 3的动画功能确实强大.以下是一个纯CSS3制作 ...
- Matrix控制平移、旋转和缩放的方法
1.setTranslate(float ds,float dy):控制Matrix进行平移.2.setSkew(float kx,float ky,float px,float py):控制Matr ...
- 使用canvas绘制渐变色矩形和使用按键控制人物移动
使用canvas绘制渐变色矩形和使用按键控制人物移动 1.使用canvas绘制渐变色矩形 效果演示 相关代码: <!DOCTYPE html> <html lang="en ...
- Easy Touch 摇感控制人物移动
Easy Touch 摇感控制人物移动 public class joystick : MonoBehaviour { public float Speed; //定义速度 p ...
- matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色
一起来学matlab-matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 < ...
随机推荐
- iptables总结
iptables: 包过滤型防火墙 Firewall: 防火墙,隔离工具:工作于主机或网络的边缘,对于进出本主机或网络的报文根据事先定义好的检查规则作匹配检测,对于能够被规则所匹配到 ...
- leetcode-806-Number of Lines To Write String
题目描述: We are to write the letters of a given string S, from left to right into lines. Each line has ...
- python学习笔记1.3
温度转换实例 #TempConvert.pyTempStr = input("请输入带有符号的温度值: ")if TempStr[-1] in ['F', 'f']:C = (ev ...
- 【http协议】浅谈
[http协议]浅谈 一. 概述 http,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议. 请求与响应: 客户端发送请求,服务器端响应数 ...
- React第二篇:组件的生命周期
前言:因为生命周期是必须要掌握的,所以React的第二篇咱就写这. (版本:16.3.2) React的生命周期大致分为四个状态:分别是Mouting.Updating.Unmounting.Erro ...
- Python——制作模块
步骤一:创建包 步骤二:编辑示例模块代码 __init__调用: 步骤三:创建setup.py from distutils.core import setup setup(name="pa ...
- Java Web基础——Controller+Service +Dao三层的功能划分
转自:https://www.cnblogs.com/cielosun/articles/5752272.html 1. Controller/Service/DAO简介: Controller是管理 ...
- Java基础29-子父类中的成员变量
/* 成员: 1.成员变量 2.函数 3.构造函数 变量: this 代表当前对象的引用 this.变量 首先在本类中找所需要的变量,如果没有找到再父类中找. super 用于访问当前对象的父类成员, ...
- springboot和quartz整合分布式多节点
虽然单个Quartz实例能给予我们很好的任务job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一 ...
- mysql并发更新问题
问题背景: 假设MySQL数据库有一张会员表vip_member(InnoDB表),结构如下: 当一个会员想续买会员(只能续买1个月.3个月或6个月)时,必须满足以下业务要求: •如果end_at ...