【问题标题】:Unable to disable a collider无法禁用对撞机
【发布时间】:2021-03-29 06:36:59
【问题描述】:

我正在做一个挖泥游戏。有 2 个泥游戏对象(精灵)需要以正确的顺序挖掘。我为每个对象分配一个脚本。禁用 mud2 boxcollider 以防止它们移动,直到泥 1 被拖动以触发 L box collider 。但是当我尝试播放时,即使mud1没有触发L box collider,mud2仍然可以拖动。我还取消了mud2的box collider。

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

public class pg7_mud1 : MonoBehaviour
{
   private Vector3 screenPoint;
   private Vector3 startPosition;

   public BoxCollider mud2;
   // Start is called before the first frame update
   void Start()
   {
       mud2.enabled = false;
   }

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

    void OnMouseDown()
   {
      
       startPosition = this.gameObject.transform.position;
       screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
       
   }

   void OnMouseDrag()
   {
      Debug.Log("can drag");
       Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
       Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
       this.transform.position = new Vector3(curPosition.x, -5f, curPosition.z);  
   }

    void OnTriggerEnter(Collider other)
   {
       if(other.gameObject.name =="left-area")
       {   
           Debug.Log("touched edge");
           mud2.enabled = true;
       }
   }

【问题讨论】:

    标签: visual-studio unity3d augmented-reality


    【解决方案1】:

    您可以尝试使用 Destroy(object, time) 方法而不是禁用碰撞器

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class pg7_mud1 : MonoBehaviour
    {
       private Vector3 screenPoint;
       private Vector3 startPosition;
    
       public BoxCollider mud2;
       public gameObject mud2obj;
       // Start is called before the first frame update
       void Start()
       {
           destroy(mud2);
       }
    
       // Update is called once per frame
       void Update()
       {
        
        
       }
    
        void OnMouseDown()
       {
          
           startPosition = this.gameObject.transform.position;
           screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
           
       }
    
       void OnMouseDrag()
       {
          Debug.Log("can drag");
           Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
           Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
           this.transform.position = new Vector3(curPosition.x, -5f, curPosition.z);  
       }
    
        void OnTriggerEnter(Collider other)
       {
           if(other.gameObject.name =="left-area")
           {   
               Debug.Log("touched edge");
               mud2obj.AddComponent(typeof(BoxCollider));
           }
       }
    

    【讨论】:

    • 像魅力一样工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2021-06-26
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多