CharacterMotor_刚体角色驱动

Unity角色移动控制
本文介绍了一个Unity中的角色移动控制脚本,包括向指定位置移动、根据速度旋转以及速度管理等功能。该脚本通过控制刚体组件实现平滑的移动效果,并提供了一系列实用的方法来调整角色的移动行为。
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
 */

 

转载于:https://www.cnblogs.com/softimagewht/p/3747787.html

标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值