unity3d 移动与旋转 2
这次的代码示例是配合动画系统使用的
4.3新的动画系统允许动画带有位置偏移,只需要在Animator组件中勾选Apply Root Motion我们就可以使用它了。
using UnityEngine;
using System.Collections; public class DonePlayerMovement : MonoBehaviour
{
public AudioClip shoutingClip; // Audio clip of the player shouting.
public float turnSmoothing = 15f; // A smoothing value for turning the player.
public float speedDampTime = 0.1f; // The damping for the speed parameter private Animator anim; // Reference to the animator component.
private DoneHashIDs hash; // Reference to the HashIDs. void Awake ()
{
// Setting up the references.
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>(); // Set the weight of the shouting layer to 1.
anim.SetLayerWeight(, 1f);
} void FixedUpdate ()
{
// Cache the inputs.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool sneak = Input.GetButton("Sneak"); MovementManagement(h, v, sneak);
} void Update ()
{
// Cache the attention attracting input.
bool shout = Input.GetButtonDown("Attract"); // Set the animator shouting parameter.
anim.SetBool(hash.shoutingBool, shout); AudioManagement(shout);
} void MovementManagement (float horizontal, float vertical, bool sneaking)
{
// Set the sneaking parameter to the sneak input.
anim.SetBool(hash.sneakingBool, sneaking); // If there is some axis input...
if(horizontal != 0f || vertical != 0f)
{
// ... set the players rotation and set the speed parameter to 5.5f.
Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
// Otherwise set the speed parameter to 0.
anim.SetFloat(hash.speedFloat, );
} void Rotating (float horizontal, float vertical)
{
// Create a new vector of the horizontal and vertical inputs.
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical); // Create a rotation based on this new vector assuming that up is the global y axis.
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); // Create a rotation that is an increment closer to the target rotation from the player's rotation.
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime); // Change the players rotation to this new rotation.
rigidbody.MoveRotation(newRotation);
} void AudioManagement (bool shout)
{
// If the player is currently in the run state...
if(anim.GetCurrentAnimatorStateInfo().nameHash == hash.locomotionState)
{
// ... and if the footsteps are not playing...
if(!audio.isPlaying)
// ... play them.
audio.Play();
}
else
// Otherwise stop the footsteps.
audio.Stop(); // If the shout input has been pressed...
if(shout)
// ... play the shouting clip where we are.
AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
}
}
unity3d 移动与旋转 2的更多相关文章
- Unity3D料槽设备旋转(一)
1.使用C#创建控制游戏对象的的脚本语言, 第一步: 在project师徒中create 一个C#脚本,将其按照自己的设备名称进行命名,这里我将其简单的命名成zhuaquanzhou.cs 使用编辑器 ...
- unity3d 移动与旋转 1
移动与旋转 1 player角色随asdw按键左右上下移动并旋转 public void Update() { // Reset player rotation to look in the same ...
- Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会
在unity3d中,用四元数来表示旋转,四元数英文名叫quaternion . 比如 transform.rotation 就是一个四元数,其由四个部分组成 Quaternion = (xi + yj ...
- 09应用输入经理旋转场景--《猿学校课程Unity3d》
为什么极品飞车游戏等.,我们可以通过系统设置非常的方面根据自己喜欢的操作模式设置,有些人喜欢用箭头来控制不喜欢与使用"W,S,A,D"控制,这就解释程序猿不会死在程序写入内部控制, ...
- Unity3D 如何图形问题修正旋转模型已导入?
如何纠正旋转模型被导入? 一些立体艺术资源包导出其模式,以便 Z 轴向上.Unity 大多数标准的脚本中假定的三维世界 Y 轴代表了.在 Unity 比改动脚本使其契合easy得多. Z 轴朝上 ...
- 【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)
[坦克大战]Unity3D多人在线游戏 http://www.taikr.com/my/course/937 1.NetworkManager的介绍: 说明:选择固定生成时会自动寻找有StartPos ...
- 【转载】Unity3D研究院之IOS触摸屏手势控制镜头旋转与缩放
前几篇文章介绍了很多Unity3D引擎自身的一些问题, 今天我们在回到IOS设备上讨论一些触摸屏幕手势,本章的目标是通过触摸iPhone屏幕手势 实现模型左右的旋转,与模型的缩放. 大家想一想模型的旋 ...
- 【转】Unity3D研究院之设置自动旋转屏幕默认旋转方向
http://www.xuanyusong.com/archives/2871 如下图所示,在处理屏幕默认旋转方向的时候可以在这里进行选择,上下左右一共是4个方向. 策划的需求是游戏采用横屏,但是要求 ...
- unity3d 触屏多点触控(旋转与缩放)
unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creati ...
随机推荐
- js enter键激发事件
document.onkeydown = function (e) { if (!e) e = window.event; if ((e.keyCode | ...
- JSON.js 源码学习..
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LaTeX 之 \label 的运用
LaTeX 之 \label 的运用 前言 大部分的LaTex教程里面都会提到 \label 的标记功能,而如果入门时就玩耍过WinEdt的同学在工具栏上点击各种环境的时候就会发现\label这个东东 ...
- apache伪静态设置
在网站根目录下新建一个.htaccess文件即可,编辑如下 RewriteEngine On #游戏列表详细介绍 RewriteRule ^g-([0-9]+).html$ game.php?acti ...
- JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
先看php文件,当我们获取到$arr这个数组后 foreach ($arr as $value) { $json .= json_encode($value) . ','; } echo '[' . ...
- servlet深探
在spring4下面使用的是ServletContext作为容器,这个是servlet规范里面设置的:加载了默认的servlet(在spring 4之前都是web.xml中做的),但是在spring ...
- Lucene/Solr搜索引擎开发笔记 - 写作方向调整
今天突然想到一个问题,觉得直接从Solr开始写,如果没有Lucene知识背景的话,看后续的章节还是比较吃力的,所以从下一篇博文开始,我可能会从Lucene开始写作,只要有Java的基础,搞定Lucen ...
- mark TODO:完善拦截规则;日志分析;web仪表盘展示;终极目标动态配置规则
- c++中头文件与实现文件的关系
转自:http://xiangyanglai.blog.163.com/blog/static/2047252022012715103338279/ 关于两者以前的关系,要从N年以前说起了~ long ...
- 黄聪:360浏览器、chrome开发扩展插件教程(1)开发Chrome Extenstion其实很简单
转载:http://www.cnblogs.com/walkingp/archive/2011/03/31/2001628.html Chrome的更新速度可以说前无古人,现在我每天开机的第一件事就是 ...