1. using UnityEngine;
  2.  
  3. //this class holds movement functions for a rigidbody character such as player, enemy, npc..
  4. //you can then call these functions from another script, in order to move the character
  5. [RequireComponent(typeof(Rigidbody))]
  6. public class CharacterMotor : MonoBehaviour
  7. {
  8. [HideInInspector]
  9. public Vector3 currentSpeed;
  10. [HideInInspector]
  11. public float DistanceToTarget;
  12.  
  13. void Awake()
  14. {
  15. rigidbody.interpolation = RigidbodyInterpolation.Interpolate;//设置插值,对于主角等可以让运动平滑
  16. rigidbody.constraints = RigidbodyConstraints.FreezeRotation;//冻结旋转
  17.  
  18. //如没有物理材质新建添加
  19. if(collider.material.name == "Default (Instance)")
  20. {
  21. PhysicMaterial pMat = new PhysicMaterial();
  22. pMat.name = "Frictionless";
  23. pMat.frictionCombine = PhysicMaterialCombine.Multiply;
  24. pMat.bounceCombine = PhysicMaterialCombine.Multiply;
  25. pMat.dynamicFriction = 0f;
  26. pMat.staticFriction = 0f;
  27. collider.material = pMat;
  28. Debug.LogWarning("No physics material found for CharacterMotor, a frictionless one has been created and assigned", transform);
  29. }
  30. }
  31. //move rigidbody to a target and return the bool "have we arrived?"
  32. public bool MoveTo(Vector3 destination, float acceleration, float stopDistance, bool ignoreY)
  33. {
  34. Vector3 relativePos = (destination - transform.position);
  35. if(ignoreY)
  36. relativePos.y = ;
  37.  
  38. //向量长度 到目标点距离
  39. DistanceToTarget = relativePos.magnitude;
  40. //运动参数
  41. if (DistanceToTarget <= stopDistance)
  42. return true;
  43. else
  44. rigidbody.AddForce(relativePos.normalized * acceleration * Time.deltaTime, ForceMode.VelocityChange);
  45. return false;
  46. }
  47.  
  48. //rotates rigidbody to face its current velocity
  49. public void RotateToVelocity(float turnSpeed, bool ignoreY)
  50. {
  51. Vector3 dir;
  52. if(ignoreY)
  53. dir = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z);
  54. else
  55. dir = rigidbody.velocity; //刚体的速度向量
  56.  
  57. if (dir.magnitude > 0.1)
  58. {
  59. Quaternion dirQ = Quaternion.LookRotation (dir);
  60. Quaternion slerp = Quaternion.Slerp (transform.rotation, dirQ, dir.magnitude * turnSpeed * Time.deltaTime);
  61. rigidbody.MoveRotation(slerp);
  62. }
  63. }
  64.  
  65. //rotates rigidbody to a specific direction
  66. public void RotateToDirection(Vector3 lookDir, float turnSpeed, bool ignoreY)
  67. {
  68. Vector3 characterPos = transform.position;
  69. if(ignoreY)
  70. {
  71. characterPos.y = ;
  72. lookDir.y = ;
  73. }
  74.  
  75. Vector3 newDir = lookDir - characterPos;
  76. Quaternion dirQ = Quaternion.LookRotation (newDir);
  77. Quaternion slerp = Quaternion.Slerp (transform.rotation, dirQ, turnSpeed * Time.deltaTime);
  78. rigidbody.MoveRotation (slerp);
  79. }
  80.  
  81. // apply friction to rigidbody, and make sure it doesn't exceed its max speed
  82. public void ManageSpeed(float deceleration, float maxSpeed, bool ignoreY)
  83. {
  84. currentSpeed = rigidbody.velocity;
  85. if (ignoreY)
  86. currentSpeed.y = ;
  87.  
  88. if (currentSpeed.magnitude > )
  89. {
  90. rigidbody.AddForce ((currentSpeed * -) * deceleration * Time.deltaTime, ForceMode.VelocityChange);
  91. if (rigidbody.velocity.magnitude > maxSpeed)
  92. rigidbody.AddForce ((currentSpeed * -) * deceleration * Time.deltaTime, ForceMode.VelocityChange);
  93. }
  94. }
  95. }
  96.  
  97. /* NOTE: ManageSpeed does a similar job to simply increasing the friction property of a rigidbodies "physics material"
  98. * but this is unpredictable and can result in sluggish controls and things like gripping against walls as you walk/falls past them
  99. * it's not ideal for gameplay, and so we use 0 friction physics materials and control friction ourselves with the ManageSpeed function instead */
  100.  
  101. /* NOTE: when you use MoveTo, make sure the stopping distance is something like 0.3 and not 0
  102. * if it is 0, the object is likely to never truly reach the destination, and it will jitter on the spot as it
  103. * attempts to move toward the destination vector but overshoots it each frame
  104. */

CharacterMotor_刚体角色驱动的更多相关文章

  1. K3Cloud开放数据模型

          金蝶K/3 Cloud是基于WEB2.0与云技术的一个开放式.社会化的新时代企业管理服务平台.整个产品采用SOA架构,完全基于BOS平台组建而成,业务架构上贯穿流程驱动与角色驱动思想,结合 ...

  2. unity3d 动画卡帧 动画合成 动画层次

    2013-02-26 16:22 2059人阅读 评论(0) 收藏 举报 unity3d 中动画的添加   http://unity3d.com/support/documentation/Manua ...

  3. 探讨Microsoft Solution Framework(MSF)框架下管理的秘密

    hello,同学们,同胞们,同志们,同龄们,这样们,那样们,们们们,我又回来写“论文”了,半年时间没见我发布任何博文,是不是认为我被潜规则了啊,哈哈.我想死你们了.好了,废话不多说,进入今天主题:   ...

  4. Actor:人生如戏全靠演技--“三维度”逻辑编程语言的设计(3)

    在上一篇介绍了逻辑编程的作用,介绍了逻辑编程中的一些概念,包括逻辑程序的结构:事实.规则和问题:知识的表达方式:谓词演算.产生式规则,以及这些概念与三维度(角色+场景+时间)理论的契合关系,正式提出了 ...

  5. 敏捷开发(Scrum)与敏捷测试

    1.敏捷测试流程和传统测试流程 软件测试是贯穿整个软件开发生命周期.对软件产品(包括阶段性产品)进行验证和确认的活动过程,也是对软件产品质量持续的评估过程,其目的是尽快尽早地发现在软件产品(包括阶段性 ...

  6. 浅谈我对DDD领域驱动设计的理解

    从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来解决. 比如,我是一家企业,然后我觉得我现在线下销售自己的产品还不够,我希望能够在线上也能销售自己的产品 ...

  7. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  8. 领域驱动设计实战—基于DDDLite的权限管理OpenAuth.net

    在园子里面,搜索一下“权限管理”至少能得到上千条的有效记录.记得刚开始工作的时候,写个通用的权限系统一直是自己的一个梦想.中间因为工作忙(其实就是懒!)等原因,被无限期搁置了.最近想想,自己写东西时, ...

  9. 我的“第一次”,就这样没了:DDD(领域驱动设计)理论结合实践

    写在前面 插一句:本人超爱落网-<平凡的世界>这一期,分享给大家. 阅读目录: 关于DDD 前期分析 框架搭建 代码实现 开源-发布 后记 第一次听你,清风吹送,田野短笛:第一次看你,半弯 ...

随机推荐

  1. win10 + linux 制作双系统教程(我本身是win10系统)

    1.制作启动U盘 准备工作: .linux镜像 .硬盘空余空间>8G,越大越好 .制作启动U盘的软件 .最好3.0U盘一个>4G 下载启动软件的工具(UItraIOS制作的U盘启动盘无法安 ...

  2. (笔记)Mysql命令use:使用数据库

    use命令可以让我们来使用数据库. use命令格式: use <数据库名>; 例如,如果xhkdb数据库存在,尝试存取它:   mysql> use xhkdb;屏幕提示:Datab ...

  3. Linux中的绝对路径和相对路径

    一.介绍 1,文件路径 什么是文件的路径? 答:这个文件存放的地方,可以联想为 文件的“家”. 在Linux中,存在着绝对路径和相对路径 绝对路径:路径的写法一定是由根目录 / 写起的,例如 /usr ...

  4. Java如何使用线程解决生产者消费者问题?

    在Java编程中,如何使用线程解决生产者消费者问题? 以下示例演示如何使用线程解决生产者消费者问题. package com.yiibai; public class ProducerConsumer ...

  5. MarkDown 使用说明示例

    一.标题 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 一级标题 这是 H2 这是 H3 一级和二级标题还有一种写法 就是下面加横杆,同时 超过2个的 = 和 - 都可以有效果. Thi ...

  6. laravel-第一課安裝

    本地安装laravel,php环境要配置好,推荐xmapp一键搭建. 1.程序包直接从官方下载,官方开源地址:https://github.com/laravel/laravel(当然也可从此网站:h ...

  7. 关于jq ajax封装以及ajax上传Webapi

    jq的ajax完整版本 $(function () {     fileChange(); }); function fileChange() {     $('#fileinput').change ...

  8. Windows 7运行命令大全

    Windows 7运行命令大全,小编整理了常用的45个Win7运行命令分享给大家:1.cleanmgr:打开磁盘清理工具 2.compmgmt.msc:计算机管理 3.conf:启动系统配置实用程序 ...

  9. (转)GCT之逻辑经验总结(拿来主义)

    GCT逻辑考试,并非考核逻辑专业知识,而是考核考生的日常逻辑思维能力.应该说日常逻辑思维能力是人在成长过程中及在社会活动中形成的,因此,只要运用好这种能力,就能取得逻辑考试的好成绩.因此可以认为:GC ...

  10. vue的手机端框架mint-ui头部header组件实现返回到上一个浏览页面

    <mt-header title="中文号主页" fixed> <router-link to="" slot="left" ...