using UnityEngine;

//this class holds movement functions for a rigidbody character such as player, enemy, npc..
//you can then call these functions from another script, in order to move the character
[RequireComponent(typeof(Rigidbody))]
public class CharacterMotor : MonoBehaviour
{
[HideInInspector]
public Vector3 currentSpeed;
[HideInInspector]
public float DistanceToTarget; void Awake()
{
rigidbody.interpolation = RigidbodyInterpolation.Interpolate;//设置插值,对于主角等可以让运动平滑
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;//冻结旋转 //如没有物理材质新建添加
if(collider.material.name == "Default (Instance)")
{
PhysicMaterial pMat = new PhysicMaterial();
pMat.name = "Frictionless";
pMat.frictionCombine = PhysicMaterialCombine.Multiply;
pMat.bounceCombine = PhysicMaterialCombine.Multiply;
pMat.dynamicFriction = 0f;
pMat.staticFriction = 0f;
collider.material = pMat;
Debug.LogWarning("No physics material found for CharacterMotor, a frictionless one has been created and assigned", transform);
}
}
//move rigidbody to a target and return the bool "have we arrived?"
public bool MoveTo(Vector3 destination, float acceleration, float stopDistance, bool ignoreY)
{
Vector3 relativePos = (destination - transform.position);
if(ignoreY)
relativePos.y = ; //向量长度 到目标点距离
DistanceToTarget = relativePos.magnitude;
//运动参数
if (DistanceToTarget <= stopDistance)
return true;
else
rigidbody.AddForce(relativePos.normalized * acceleration * Time.deltaTime, ForceMode.VelocityChange);
return false;
} //rotates rigidbody to face its current velocity
public void RotateToVelocity(float turnSpeed, bool ignoreY)
{
Vector3 dir;
if(ignoreY)
dir = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z);
else
dir = rigidbody.velocity; //刚体的速度向量 if (dir.magnitude > 0.1)
{
Quaternion dirQ = Quaternion.LookRotation (dir);
Quaternion slerp = Quaternion.Slerp (transform.rotation, dirQ, dir.magnitude * turnSpeed * Time.deltaTime);
rigidbody.MoveRotation(slerp);
}
} //rotates rigidbody to a specific direction
public void RotateToDirection(Vector3 lookDir, float turnSpeed, bool ignoreY)
{
Vector3 characterPos = transform.position;
if(ignoreY)
{
characterPos.y = ;
lookDir.y = ;
} Vector3 newDir = lookDir - characterPos;
Quaternion dirQ = Quaternion.LookRotation (newDir);
Quaternion slerp = Quaternion.Slerp (transform.rotation, dirQ, turnSpeed * Time.deltaTime);
rigidbody.MoveRotation (slerp);
} // apply friction to rigidbody, and make sure it doesn't exceed its max speed
public void ManageSpeed(float deceleration, float maxSpeed, bool ignoreY)
{
currentSpeed = rigidbody.velocity;
if (ignoreY)
currentSpeed.y = ; if (currentSpeed.magnitude > )
{
rigidbody.AddForce ((currentSpeed * -) * deceleration * Time.deltaTime, ForceMode.VelocityChange);
if (rigidbody.velocity.magnitude > maxSpeed)
rigidbody.AddForce ((currentSpeed * -) * deceleration * Time.deltaTime, ForceMode.VelocityChange);
}
}
} /* NOTE: ManageSpeed does a similar job to simply increasing the friction property of a rigidbodies "physics material"
* but this is unpredictable and can result in sluggish controls and things like gripping against walls as you walk/falls past them
* it's not ideal for gameplay, and so we use 0 friction physics materials and control friction ourselves with the ManageSpeed function instead */ /* NOTE: when you use MoveTo, make sure the stopping distance is something like 0.3 and not 0
* if it is 0, the object is likely to never truly reach the destination, and it will jitter on the spot as it
* attempts to move toward the destination vector but overshoots it each frame
*/

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. git远程库与本地联系报错fatal: Not a git repository (or any of the parent directories): .git

    在github上新建了一个仓库,然后相与本地的仓库联系起来 $ git remote add origin https://github.com/liona329/learngit.git fatal ...

  2. 【转】25个非常实用的jQuery/CSS3应用组件

    今天分享25款功能十分强大的jQuery/CSS3应用插件,欢迎收藏. 1.jQuery水晶样式下拉导航 这是一款非常不错的jQuery多功能下拉菜单插件,菜单外观呈水晶样式,晶莹剔透,功能丰富,包含 ...

  3. 计蒜客2018 蓝桥杯省赛 B 组模拟赛(一)

    1,结果填空:年龄 今天蒜头君带着花椰妹和朋友们一起聚会,当朋友们问起年龄的时候,蒜头君打了一个哑谜(毕竟年龄是女孩子的隐私)说:“我的年龄是花椰妹年龄个位数和十位数之和的二倍”. 花椰妹看大家一脸懵 ...

  4. SpringBoot系列一:SpringBoot的产生

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 长期以来 Java 的开发一直让人所诟病: ·Java 项目开发复杂度极其高: · Java 项目的维护非常困难: · 在云时代 ...

  5. Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    严重:   Error   configuring   application   listener   of   class   org.springframework.web.context.Co ...

  6. Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点

    上一篇分享了 Nginx + Tomcat 反向代理 负载均衡 集群 部署指南,感觉还是相当实用型的,但是一般集群部署是基于大访问量的,可能有的企业用不到,类似一些企业官网,访问量并不是很大,基于这个 ...

  7. linux任务计划 chkconfig工具 systemd管理服务 unit介绍 target介绍

    linux任务计划 任务计划:特定时间备份数据,重启服务,shell脚本,单独的命令等等. 任务计划配置文件:cat /etc/crontab [root@centos7 ~]# cat /etc/c ...

  8. wine for MacOS

    Installing Wine on Mac This tutorial is for intermediate users who want to install and use Wine on t ...

  9. Oracle统计每条数据的大小

    怎么查询一条记录到底占了多少空间呢,随便用一个表举例(如上图),就着解决眼前问题的原则(oracle),网上简单查了查,发现生效了,就没深入了解了,包括其它数据库怎么解决,都没做研究.Oracle下, ...

  10. 在 ubuntu 【6.06、6.10】 上安装 oracle 10.2.0.1,并打补丁 10.2.0.5

    特点: ubuntu 6.06.6.10 算是很古老的ubuntu了,其应该是基于 debian 4 的 tesing/unstable 分支.所以,毛病较多. 如何安装oracle10g? 几个技术 ...