如何让“Baut”对象随机粘贴到“TempatBaut”和“TempatBaut2”上?

问题描述 投票:0回答:1

我有 3 个对象,他们的名字:

  1. Obeng = 带来“Baut”的对象
  2. Baut = 我想用“Obeng”移动到目标的对象
  3. TempatBaut = Baut 的目标
  4. TempatBaut2 = Baut 的其他目标

当我抓住“Obeng”并靠近“Baut”时,它们会互相粘住(当然它们有对撞机)。然后,当我将它们带到“TempatBaut”时,对象“Baut”将坚持“TempatBaut”。但我希望对象“Baut”可以坚持超过 1 个目标。我希望 Baut 可以随机粘贴到“TempatBaut”或“TempatBaut2”。

注意:*我使用 C# 在 UNITY 中制作

我的脚本:

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;
        }
    }
}```

i want Object "Baut" can stick more than 1 Target. I want Baut can stick randomly to "TempatBaut" or "TempatBaut2.
c# unity3d
1个回答
0
投票

你可以为此使用 gameObject 标签。

创建一个名为“Obeng”的游戏对象标签和另一个名为“TargetOfBaut”的标签。在 Obeng 游戏对象中,将标签设置为“Obeng”,在 TempatBaut 和 TempatBaut2 游戏对象中将其标签设置为“TargetOfBaut”。使用这个标签,您可以控制如何粘贴 Baut 游戏对象

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Obeng")
    {
        baut_masih_di_obeng = true;
    }
    else if (other.gameObject.tag == "TargetOfBaut")
    {
        sudah_ditempat_baut = true;
    }
}

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "Obeng")
    {
        baut_masih_di_obeng = false;
    }
    else if (other.gameObject.tag == "TargetOfBaut")
    {
        sudah_ditempat_baut = false;
    }
}

private void OnTriggerStay(Collider other)
{
    if (other.gameObject.tag == "Obeng" && !this.sudah_ditempat_baut)
    {
        Baut.position = Obeng.position;
        Baut.rotation = Obeng.rotation;
    }
    else if (other.gameObject.tag == "TargetOfBaut")
    {
        Baut.position = other.gameObject.position;
        Baut.rotation = other.gameObject.rotation;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.