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

 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. iOS核心动画CALayer和UIView

    UIView和CALayer的关系. 每一个UIview都有一个CALayer实例的图层属性,也就是所谓的backing layer. 实际上这些背后关联的图层才是真正用来在屏幕上显示和做动画,UIV ...

  2. 扩展欧拉定理【洛谷P4139】 上帝与集合的正确用法

    P4139 上帝与集合的正确用法 \(2^{2^{2^{\dots}}}\bmod p\) 卡最优解倒数第一祭. 带一下扩展欧拉定理就好了. code: #include <iostream&g ...

  3. openstack的部署与运维

    来公司几个月了,除了搭建了kvm虚拟机,使用3台虚拟机组合成一个openstack的网络环境.还没有正式将openstack搭建起来过.时间都在开发web程序.不过openstack也是要学习的.只能 ...

  4. day0201

    #1.使用while循环输入 1 2 3 4 5 6 8 9 10'''count = 0while count < 10: count += 1 # count = count + 1 if ...

  5. numpy的一维线性插值函数

    前言:      在用生成对抗网络生成二维数据点的时候遇到代码里的一个问题,就是numpy中的一维线性插值函数interp到底是怎么用的,在这个上面费了点功夫,因此现将其用法给出.      在生成对 ...

  6. 强制css属性生效

    今天在写一个隐藏滚动条的css时,设置的overflow-x: hidden; overflow-y: scroll;属性死活不生效, 在谷歌浏览器中查看,发现这两条属性是被划掉的.如图. 这表明别处 ...

  7. 分享一下Ubuntu好用的源

    vim /etc/apt/sources.list 然后用G跳转到最后一行,然后[ESC]切换到命令行模式,然后键入[o](表示在当前行后插入).也可以多插入几个空行.这样可以有条理的和系统默认的区分 ...

  8. Q844 比较含退格的字符串

    给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...

  9. css提取数据2个常用方法

    提取标签里的内容 所谓数据就是HTML里标签的内容,如下面红色字体,就是标签内容 <title>我只是个实验 - SCRAPY</title> 提取标签里的数据,标签可以是ti ...

  10. rabbitmq 消息确认

    消息确认主要用在接收方 如果接收方没有确认, broker可以重发,确保消息至少消息一次..