关于Rigidbody控制

using UnityEngine;

public class ConstantBounceForce : MonoBehaviour
{
    public float bounceForce = 10f; // 反弹力大小

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // 当刚体碰撞到其他物体时触发
    void OnCollisionEnter(Collision collision)
    {
        // 获取碰撞法线,即与碰撞物体接触的表面的法线方向
        Vector3 normal = collision.contacts[0].normal;
        // 计算反向力的方向,即碰撞法线的反方向
        Vector3 reverseForceDirection = -normal;
        // 施加反向力到刚体上,并且忽略摩擦力的影响
        rb.AddForce(reverseForceDirection * bounceForce, ForceMode.Impulse);
        rb.velocity = reverseForceDirection * bounceForce;
    }
}

20240412_111920

加上

using UnityEngine;

public class Buoyancy : MonoBehaviour
{
    public Transform targetPoint; // 汇集点
    public float maxBuoyancyForce = 10f; // 最大浮力大小
    public float maxDistance = 5f; // 最大作用距离

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.useGravity = false; // 关闭重力
    }

    void FixedUpdate()
    {
        // 计算物体到汇集点的距离
        float distanceToTarget = Vector3.Distance(transform.position, targetPoint.position);

        // 计算浮力大小,距离越近,浮力越大
        float buoyancyForce = Mathf.Clamp(1f - (distanceToTarget / maxDistance), 0f, 1f) * maxBuoyancyForce;

        // 计算浮力方向,向汇集点施加力
        Vector3 buoyancyDirection = (targetPoint.position - transform.position).normalized;

        // 添加浮力到物体上
        rb.AddForce(buoyancyDirection * buoyancyForce);
    }
}

20240412_112948

加上

using UnityEngine;

public class ConstantAngularVelocityOnCollision : MonoBehaviour
{
    public float desiredAngularSpeed = 30f; // 指定的角速度大小
    public float interpolationSpeed = 0.1f; // 插值速度

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    Vector3 desiredAngularVelocity;
    private void FixedUpdate()
    {
        // 计算刚体当前角速度的大小
        float currentAngularSpeed = rb.angularVelocity.magnitude;
        // 如果当前角速度大小不为零,计算新的角速度大小,并保持原来的方向
        if (currentAngularSpeed != 0)
        {
            // 计算当前角速度的单位向量
            Vector3 currentAngularDirection = rb.angularVelocity.normalized;
            // 计算新的角速度大小,并保持原来的方向
            Vector3 newAngularVelocity = currentAngularDirection * desiredAngularSpeed * Mathf.Deg2Rad;
            // 应用新的角速度到刚体上
            desiredAngularVelocity = newAngularVelocity;
        }
        // 如果当前角速度大小为零,直接设定指定的角速度大小
        else
        {
            desiredAngularVelocity = transform.up * desiredAngularSpeed * Mathf.Deg2Rad;
        }
        rb.angularVelocity = Vector3.Lerp(rb.angularVelocity, desiredAngularVelocity, interpolationSpeed);
    }
}

20240412_113157

### Unity Rigidbody 组件使用教程 #### 添加力的方法 对于带有刚体(Rigidbody)的物体,在Unity中施加力的主要方法是通过`Rigidbody.AddForce()`函数。此函数存在多个版本,允许开发者根据需求灵活应用: - `void Rigidbody.AddForce(Vector3 force)`:向对象施加线性力量[^1]。 - `void Rigidbody.AddForce(Vector3 force, ForceMode mode)`:除了指定方向和大小外,还能够定义如何解释这个力的作用模式,比如作为即时冲量还是持续作用力等。 下面是一段简单的C#脚本示例,展示了如何在一个游戏对象上添加向前的力量: ```csharp using UnityEngine; public class AddForceExample : MonoBehaviour { public float speed = 10f; void FixedUpdate() { GetComponent<Rigidbody>().AddForce(transform.forward * speed); } } ``` #### 实现平滑旋转效果 为了让物体实现类似于现实中带惯性的加速减速转动,可以利用`Rigidbody.AddTorque()`来达到目的。该命令同样接受两个参数——扭矩矢量以及可选的力矩模式[^2]。 这里给出一段用于使玩家控制的角色绕Y轴平稳自转的例子: ```csharp using UnityEngine; public class SmoothRotation : MonoBehaviour { private Rigidbody rb; public float rotationSpeed = 50f; void Start(){ rb = GetComponent<Rigidbody>(); } void Update () { if (Input.GetKey(KeyCode.LeftArrow)){ rb.AddTorque(0, -rotationSpeed*Time.deltaTime , 0 ); }else if(Input.GetKey(KeyCode.RightArrow)){ rb.AddTorque(0, rotationSpeed*Time.deltaTime , 0 ); } } } ``` #### 碰撞体与触发器的应用场景区分 在设计物理交互时,理解碰撞体(Collider)和触发器(Trigger)之间的差异非常重要。前者主要用于处理真实的物理碰撞事件;后者则更适合用来创建不阻碍运动却能感知其他物体进入其范围内的虚拟边界[^3]。 例如,当希望角色穿过一扇门时不受到阻挡但又能激活开门动画,则应设置门上的Collider为Trigger属性,并编写相应的逻辑响应进出事件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值