CharacterMotor_刚体角色驱动


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 = 0;

//向量长度 到目标点距离
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 = 0;
lookDir.y = 0;
}

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 = 0;

if (currentSpeed.magnitude > 0)
{
rigidbody.AddForce ((currentSpeed * -1) * deceleration * Time.deltaTime, ForceMode.VelocityChange);
if (rigidbody.velocity.magnitude > maxSpeed)
rigidbody.AddForce ((currentSpeed * -1) * 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_刚体角色驱动

时间: 2024-08-09 00:52:29

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

《游戏引擎架构》笔记十二

碰撞及刚体动力学 一些碰撞/物理系统:http://www.gamedev.net/community/forums/topic.asp?topic_id=463024 I-Collide:http://cs.unc.edu/I-COLLIDE SWIFT: ODE:http://www.ode.org Bullet:http://code.google.com/p/bullet/ TrueAxis:http://trueaxis.com/ PhysX: Havok: PAL:http://ww

【智能家居篇】wifi驱动的理解(3)——usb接口在wifi模块中的角色

转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 上一篇文章已经提到USB接口在wifi模块中的最重要两个函数是usb_read_port()和usb_write_port().那它们是怎么和wifi扯上关系的呢?我们可以从以下三个方面去分析: 1.首先需要明确wifi模块是USB设备,主控(CPU)端是USB主机: 2.USB主机若需要对wifi模块进行数据的读写时,就必须经过USB接口: 3.既然涉及到数据的读写操作,必然要用相应的读写函数,那么usb_re

微软要做用云量挖掘机,以技术驱动数字化转型快公司

今年7月,首次更名为"Inspire"的微软WPC全球合作伙伴大会上,微软宣布将所有与合作伙伴相关的角色都重新整合为一个新的部门:统一商业合作伙伴部门(One Commercial Partner),并进行了一整套的组织和流程改组,以适应云计算时代的用户需求与"用云量"规律. 2017年9月12日,微软大中华区副总裁.全球渠道事业部总经理.商业客户事业部总经理包嘉峰与媒体分享了这两个月微软商业合作伙伴部转型以来,微软自身所发生的变化以及为客户所带来的价值.根据包嘉峰

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

写在前面 插一句:本人超爱落网-<平凡的世界>这一期,分享给大家. 阅读目录: 关于DDD 前期分析 框架搭建 代码实现 开源-发布 后记 第一次听你,清风吹送,田野短笛:第一次看你,半弯新湖,鱼跃翠堤:第一次念你,燕飞巢冷,释怀记忆:第一次梦你,云翔海岛,轮渡迤逦:第一次认你,怨江别续,草桥知己:第一次怕你,命悬一线,遗憾禁忌:第一次悟你,千年菩提,生死一起. 人生有很多的第一次:小时候第一次牙牙学语.第一次学蹒跚学步...长大后第一次上课.第一次逃课.第一次骑自行车.第一次懂事.第一次和喜

Rigidbody(刚体) and Collider(碰撞器)

关于Rigidbody,手册上是这么描述的: Control of an object's position through physics simulation. 通过物理模拟控制一个物体的位置. Rigidbody components take control over an object's position - it makes the objects fall down under the influence of gravity, and can calculate how obj

Rigidbody(刚体)方法的初步学习(一)

概要:这次将简单的了解Rigidbody中的各种方法属性,以官方的API为顺序研究. 蛮牛API翻译:Rigidbody组件控制物体的位置-它使物体在重力影响下下落,并可计算物体将怎样响应碰撞.当操作刚体参数的时候,你应该在FixedUpdate函数中使用它,物理模拟以离散的时间步执行.FixedUpdate函数在每一步之前被立即调用. 注意:如果你的模拟看起来像慢动作并且不真实:这是缩放的问题.如果你的游戏世界非常大,所有的东西将显示移动非常慢,确保你所有模型为真实世界的大小. 例如:一个汽车

探讨DDD中角色权限与DCI的使用

本文初衷 之前在学习DDD的时候,一直被权限与角色困扰. 我们知道在Asp.net MVC 的Controller或Action加上特性标签[Authorize],就可以实现权限控制. [Authorize(Roles ="Manager")] public class MainController : Controller { // GET: Main public ActionResult Index() { return View(); } } 但是在应用层中,如何使用好角色?在

DDD领域驱动设计基本理论知识总结

领域驱动设计之领域模型 加一个导航,关于如何设计聚合的详细思考,见这篇文章. 2004年Eric Evans 发表Domain-Driven Design –Tackling Complexity in the Heart of Software (领域驱动设计),简称Evans DDD.领域驱动设计分为两个阶段: 以一种领域专家.设计人员.开发人员都能理解的通用语言作为相互交流的工具,在交流的过程中发现领域概念,然后将这些概念设计成一个领域模型:由领域模型驱动软件设计,用代码来实现该领域模型:

(转载)浅谈我对DDD领域驱动设计的理解

原文地址:http://www.cnblogs.com/netfocus/p/5548025.html 从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来解决. 比如,我是一家企业,然后我觉得我现在线下销售自己的产品还不够,我希望能够在线上也能销售自己的产品.所以,自然而然就想到要做一个普通电商系统,用于实现在线销售自己企业产品的目的. 再比如,我是一家互联网公司,公司有很多系统对外提供服务,面向很多客户端设备.但是最近由于各种原因,导致服务经常出故