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 参考书籍 < ...
随机推荐
- [ActionScript 3.0] 使用Embed在类中嵌入字体
package { import flash.display.Sprite; import flash.text.Font; import flash.text.TextField; import f ...
- redis 3.0 集群__监控警报工具(sentinel)
参考文档 http://redis.readthedocs.org/en/latest/topic/sentinel.html 因为目前还处于开发阶段,就先不研究了,待续
- iOS核心动画之anchorpoint
anchorpoint是什么 All geometric manipulations to the view occur about the specified point 就是说所有的动画参考点都是 ...
- C# - Common Tool
Json 涉及命名空间 using System.IO; using System.Net; using System.Runtime.Serialization.Json; using Newton ...
- 多个tomcat一起运行
1.默认为:8005 2.在D:\apache-tomcat-7.0.85\bin目录下,找到startup.bat,打开并修改. 3.在D:\apache-tomcat-7.0.85\bin目录下, ...
- MySql安装错误代码1045的解决方案
1.MySql安装错误代码1045的解决方案 2.root密码忘记1045的解决方案 错误代码 1045 Access denied for user 'root'@'localhost' (usin ...
- mysql里面 limit的奇效
项目里面有遇到一个需求,查询一个表,先group by ,再按group 的count(*)进行倒序,取出每个group里面发表时间最新的一个纪录,之前的同事SQL是这样写的 SELECT * FRO ...
- SpringBoot学习(一)
一.Spring Boot Spring是JavaEE轻量级代替品.无需开发重量级的(EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的POJO对 ...
- jmeter安装部署、maven路径配置
jmeter下载地址: https://jmeter.apache.org/download_jmeter.cgi 解压文件 配置jmeter环境变量 (1)设置jmeter解压目录的JMETER_H ...
- Comparing deep learning frameworks: Tensorflow, CNTK, MXNet, & Caffe
https://imaginghub.com/blog/10-a-comparison-of-four-deep-learning-frameworks-tensorflow-cntk-mxnet-a ...