代码源自噩梦射手,记录一下方便后续使用,顺便将老师的解释给备注上去_(:з」∠)_

 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之如何控制人物移动、旋转和动画播放的更多相关文章

  1. [Unity3D]Unity3D游戏开发之使用EasyTouch虚拟摇杆控制人物移动

    大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei.今天呢,我们来一起学习在Unity3D中使用EasyTouch虚拟摇杆来控制人物移动.虽然Un ...

  2. (转)在Unity3D中控制动画播放

    用Unity3D也算是好久了,但是每次做项目总还是能学到新的东西.这次做一个TPS的项目就遇到了这样一个问题,如何同时在上下半身播放不同的动画?解决方法其实是很简单,但由于对于动画资源的了解不足导致问 ...

  3. Unity学习笔记_控制人物移动+摄像机跟随

    我想做的移动操作方式类似[流星蝴蝶剑].[龙之谷].[我的世界第三人称]的第三人称操作方式. 操作说明:W键会朝当前镜头方向前进,鼠标控制镜头旋转. 做前需知(先去稍微了解一下比较好): ①unity ...

  4. iOS 控制单个控制器旋转

    iOS 控制单个控制器旋转 控制单个ViewController 的旋转 //不旋转,保持竖屏 //iOS 5 - (BOOL) shouldAutorotateToInterfaceOrientat ...

  5. 可控制转速CSS3旋转风车特效

    以前制作网页动画一般使用javascript,现在已经有越来越多动动画使用纯CSS实现,并且动画的控制也可以使用CSS3实现,因为CSS 3来了,CSS 3的动画功能确实强大.以下是一个纯CSS3制作 ...

  6. Matrix控制平移、旋转和缩放的方法

    1.setTranslate(float ds,float dy):控制Matrix进行平移.2.setSkew(float kx,float ky,float px,float py):控制Matr ...

  7. 使用canvas绘制渐变色矩形和使用按键控制人物移动

    使用canvas绘制渐变色矩形和使用按键控制人物移动 1.使用canvas绘制渐变色矩形 效果演示 相关代码: <!DOCTYPE html> <html lang="en ...

  8. Easy Touch 摇感控制人物移动

    Easy Touch 摇感控制人物移动 public class joystick : MonoBehaviour { public float Speed;             //定义速度 p ...

  9. matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色

    一起来学matlab-matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 < ...

随机推荐

  1. Python strip()函数用法

    Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数: strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格 具体示 ...

  2. js导出页面的表格到excel(NB的大神洗了好几个,挑一个记下来)

    var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.in ...

  3. 一次http请求响应流程

    前端客户端 发起http请求 web服务器接收并解析http报文 通过WSGI协议发送给web框架 web框架创建请求对象 中间层处理 具体的视图处理-业务处理 中间层处理 创建http响应对象 返回 ...

  4. 题目1018:统计同成绩学生人数(hash简单应用)

    问题来源 http://ac.jobdu.com/problem.php?pid=1018 问题描述 给你n位同学的成绩,问获得某一成绩的学生有多少位. 问题分析 初见此题,有人会想到先将所有成绩存入 ...

  5. TypeScript学习小结

    TypeScript是什么 TypeScript是由微软公司开发的一个开源JavaScript的超集,主要提供了类型系统和对ES6的支持,可以编译成纯JavaScript,主要就是对JavaScrip ...

  6. oracle12c之四 控制PDB操作 PDBLockdown Profiles

    除了IO.内存.CPU之外,还有一些限制,比如:限制在pdb中的操作命令,我们可以创建一个lockdown profile来限制对当前PDB的操作,增强某些操作的安全性.   关于PDB Lockdo ...

  7. Rancher 1.6 版本 只能在 linux 下用

    实际操作 启动 , 访问方式 : 在启动过程中会发现没有 image , 然后自动下载 ( 执行 docker pull 命令 ) docker run --rm --privileged -v /v ...

  8. 实验Complex

    #include<iostream> #include<cmath> using namespace std; class Complex { public: Complex ...

  9. 《大数据日知录》读书笔记-ch2数据复制与一致性

    CAP理论:Consistency,Availability,Partition tolerance 对于一个分布式数据系统,CAP三要素不可兼得,至多实现其二.要么AP,要么CP,不存在CAP.分布 ...

  10. Docker搭建tomcat运行环境(Dockerfile方式)

    上一篇文章的基本做法是通过centOS的官方镜像启动一个容器,然后进入到容器中,手动敲命令安装JDK跟tomcat,这个跟在linux下搭建没有什么区别,只是用来熟悉docker命令,并且在日常开发中 ...