You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中如何让Baut对象随机吸附到TempatBaut或TempatBaut2?

Unity C# 脚本修改:让Baut随机吸附到两个目标位置

场景对象说明

  • Obeng:可携带Baut的对象
  • Baut:需要通过Obeng移动到目标的对象
  • TempatBaut:Baut的目标位置之一
  • TempatBaut2:Baut的另一个目标位置

当前功能与需求

当前已实现:抓取Obeng并靠近Baut时,二者通过碰撞体相互吸附;将它们带到TempatBaut时,Baut会吸附到该位置。
需要修改:让Baut能够随机吸附到TempatBaut或TempatBaut2。

原脚本

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

public class RotasiObengBautUjiCoba : MonoBehaviour
{
    private bool baut_masih_di_obeng = false;
    private bool sudah_ditempat_baut = false;

    [Header("Titik Baut")]
    public Transform Baut;

    [Header("Target Baut")]
    public Transform Obeng;

    [Header("Tempat Baut")]
    public Transform TempatBaut;
    public Transform TempatBaut2;

    [Header("Sound")]
    public AudioSource suaraPlug;

    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "Baut")
        {
            suaraPlug.Play();
        }
    }


    private void OnTriggerStay(Collider other)
    {
        if (other.name == "Baut")
        {
            if (!this.baut_masih_di_obeng)
            {
                Baut.position = Obeng.position;
                Baut.rotation = Obeng.rotation;
            }
            else if (!this.sudah_ditempat_baut)
            {
                other.transform.position = TempatBaut.position;
                other.transform.rotation = TempatBaut.rotation;

                sudah_ditempat_baut = true;
            }
        }

        if (other.name == "TempatBaut")
        {
            this.baut_masih_di_obeng = true;
        }
    }
}

修改后的脚本

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

public class RotasiObengBautUjiCoba : MonoBehaviour
{
    private bool baut_masih_di_obeng = false;
    private bool sudah_ditempat_baut = false;
    // 存储随机选中的目标位置
    private Transform randomTargetBaut;

    [Header("Titik Baut")]
    public Transform Baut;

    [Header("Target Baut")]
    public Transform Obeng;

    [Header("Tempat Baut")]
    public Transform TempatBaut;
    public Transform TempatBaut2;

    [Header("Sound")]
    public AudioSource suaraPlug;

    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "Baut")
        {
            suaraPlug.Play();
        }
        // 触发任意目标位置时,提前随机选定目标
        if ((other.name == "TempatBaut" || other.name == "TempatBaut2") && !sudah_ditempat_baut)
        {
            this.baut_masih_di_obeng = true;
            // 50%概率随机选择其中一个目标位置
            randomTargetBaut = Random.value > 0.5f ? TempatBaut2 : TempatBaut;
        }
    }


    private void OnTriggerStay(Collider other)
    {
        if (other.name == "Baut")
        {
            if (!this.baut_masih_di_obeng)
            {
                Baut.position = Obeng.position;
                Baut.rotation = Obeng.rotation;
            }
            else if (!this.sudah_ditempat_baut && randomTargetBaut != null)
            {
                other.transform.position = randomTargetBaut.position;
                other.transform.rotation = randomTargetBaut.rotation;

                sudah_ditempat_baut = true;
            }
        }
    }
}

修改说明

  1. 新增变量:添加randomTargetBaut存储随机选中的目标,避免重复随机导致Baut位置跳变。
  2. 触发逻辑调整:在触发任意一个目标位置时,完成随机目标的选择,同时标记baut_masih_di_obeng为true。
  3. 吸附目标替换:将Baut的吸附目标从固定的TempatBaut改为提前选中的随机目标。
  4. 空值判断:添加randomTargetBaut != null的判断,避免未触发目标时出现空引用错误。

内容的提问来源于stack exchange,提问作者Ketut Suadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 10:55:10