【问题标题】:How to detect if the mouse is over the object Unity C#如何检测鼠标是否在对象Unity C#上
【发布时间】:2020-09-14 18:56:59
【问题描述】:

我一直在开发 Unity 3D 游戏,其中涉及能够拾取一些物体。我目前的代码如下:

using System.Collections;

使用 System.Collections.Generic; 使用 UnityEngine;

public class pickUp : MonoBehaviour
{
  public Transform dest;
  void OnMouseDown() {
      GetComponent<Rigidbody>().useGravity = false;
      GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
      GetComponent<Rigidbody>().angularVelocity = new Vector3(0f, 0f, 0f);
      this.transform.position = dest.position;
      this.transform.parent = GameObject.Find("Destination").transform;
    }

  void OnMouseUp() {
    this.transform.parent = null;
    GetComponent<Rigidbody>().useGravity = true;
  }
  void Update() {
//where I need to detect mouseover    
  }
}

正如评论中所暗示的那样,我需要检测鼠标是否在对象上,否则它会关闭所有其他对象的物理。我也无法编写名称(所以使用 Ray),因为很多对象已经使用了该脚本。

谢谢!

编辑:旧版本代码,放入当前版本

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您的原始问题代码如下所示

    void Update() 
    {
        if (Input.GetMouseButton(0)) 
        {
            void OnMouseOver() 
            {
                //where I need to detect mouseover 
            }
        }
    }
    

    几乎就在那里,但当然将其声明为Update 内的嵌套方法是没有意义的!

    你的脚本应该看起来像

    public class pickUp : MonoBehaviour
    {
        public Transform dest;
    
        // You should already set this via the Inspector
        [SerializeField] private Rigidbody _rigidbody;
    
        private void Awake()
        {
            // or as fallback get it ONCE
            if(!_rigidbody) _rigidbody = GetComponent<Rigidbody>();
        }
    
        private void OnMouseDown() 
        {
            _rigidbody.useGravity = false;
            _rigidbody.velocity = Vector3.zero;
            _rigidbody.angularVelocity = Vector3.zero;
    
            transform.position = dest.position;
            transform.parent = GameObject.Find("Destination").transform;
        }
    
        private void OnMouseUp() 
        {
            transform.parent = null;
            _rigidbody.useGravity = true;
        }
    
        // Called every frame while the mouse stays over this object
        private void OnMouseOver() 
        {
            if (Input.GetMouseButton(0)) 
            {
                // whatever shall happen every frame while mouse over and mouse button 0 stays pressed
            }
        }
    }
    

    或者如果你只需要在鼠标进入并存在对象的那一刻做某事,而是使用

    private void OnMouseEnter()
    {
    
    }
    
    private void OnMouseExit()
    {
    
    }
    

    【讨论】:

    • 好吧,我知道我做了什么。谢谢!
    【解决方案2】:

    我认为你遗漏了一些东西,void OnMouseDown()、OnMouseOver()、OnMouseUp(),只调用具有对撞机和附加脚本的特定对象,无论你使用多少次相同多个对象的脚本。这些方法独立于每个对象。

    【讨论】:

    • 我需要在每一帧都检测到这一点,并且 OnMouseOver 和 OnMouseDown 仅在它们发生时才被调用。不能在 Update() 中调用它们。
    • 我明白你的意思,鼠标悬停不正确,我正在修复它(复制+粘贴旧版本)
    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多