Unity Rigidbody组件

Unity 学习笔记汇总
Rigidbody官方API使用文档

1. 碰撞器1

1.1. 前台

  • 创建GameObject\3D Object\CubeGameObject\3D Object\SphereGameObject\3D Object\Plane
  • CubeSphere添加Component\Physics\Rigidbody
  • 将C#脚本挂载到Cube

在这里插入图片描述

1.2. 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("检测到了碰撞");
    }

    void OnCollisionStay(Collision collision)
    {
        Debug.Log("碰撞停留");
    }

    void OnCollisionExit(Collision collision)
    {
        Debug.Log("碰撞结束");
    }
}

1.3. 结果

在控制台上会实时输出碰撞状态。

2. 碰撞器2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("检测到了碰撞");

        //collision: 指的是身上没有该函数脚本的那个物体
        Debug.Log(collision.gameObject.name);
        Destroy(collision.gameObject);  //销毁的是未挂载当前脚本的物体
        Destroy(gameObject);  //销毁的是挂载当前脚本的物体
    }

    void OnCollisionStay(Collision collision)
    {
        //Debug.Log("碰撞停留");
    }

    void OnCollisionExit(Collision collision)
    {
        Debug.Log("碰撞结束");
    }
}

3. 碰撞器3

3.1. 前台

创建一个Capsule的预制体,并与脚本中的变量进行绑定
在这里插入图片描述

3.2. 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public GameObject contractPrefab;
    GameObject clone;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnCollisionEnter(Collision collision)
    {
        //Debug.Log("检测到了碰撞");

        collision: 指的是身上没有该函数脚本的那个物体
        //Debug.Log(collision.gameObject.name);
        //Destroy(collision.gameObject);  //销毁的是未挂载当前脚本的物体
        //Destroy(gameObject);  //销毁的是挂载当前脚本的物体
        ContactPoint[] points = collision.contacts;
        if (clone == null)
        {
            clone = Instantiate(contractPrefab, points[0].point,
                                            contractPrefab.transform.rotation) as GameObject;
        }
        else
        {
            Destroy(clone.gameObject, 0.5f);
            //clone = Instantiate(contractPrefab, points[0].point,
            //                                contractPrefab.transform.rotation) as GameObject;
        }

    }

    void OnCollisionStay(Collision collision)
    {
        //Debug.Log("碰撞停留");
    }

    void OnCollisionExit(Collision collision)
    {
        Debug.Log("碰撞结束");
    }
}

3.3. 结果

Sphere发生碰撞后,会生成一个Capsule的预制体

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果不使用Unity Rigidbody组件,可以通过手动计算物体的运动来实现运动约束。以下是一个简单的示例代码,演示如何手动计算物体的运动并实现运动约束。 ``` using UnityEngine; public class ConstraintsDemo : MonoBehaviour { private Vector3 velocity; // Start is called before the first frame update void Start() { velocity = Vector3.zero; } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space)) { // 禁止物体在Y轴上移动 velocity.y = 0; } if (Input.GetKeyDown(KeyCode.R)) { // 禁止物体旋转 transform.rotation = Quaternion.identity; } if (Input.GetKeyDown(KeyCode.F)) { // 禁止物体在X轴和Z轴上移动,但允许旋转 velocity.x = 0; velocity.z = 0; } if (Input.GetKeyDown(KeyCode.C)) { // 取消所有约束 velocity = Vector3.zero; } // 根据速度更新物体的位置 transform.position += velocity * Time.deltaTime; } } ``` 在这个示例中,我们通过手动计算物体的速度来控制物体的运动。通过修改速度的不同分量,可以实现不同的运动约束效果。例如,通过将速度的Y分量设置为0,可以禁止物体在Y轴上移动;通过将速度的X和Z分量设置为0,可以禁止物体在X轴和Z轴上移动,但允许旋转。最后,通过将速度设置为0,可以取消所有约束。需要注意的是,在手动计算物体运动时,需要手动添加重力和碰撞检测等功能,以便更加真实地模拟物理运动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

COCO56(徐可可)

建议微信红包:xucoco56

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值