Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检测鼠标是否在对象上#_C#_Unity3d - Fatal编程技术网

C# 如何检测鼠标是否在对象上#

C# 如何检测鼠标是否在对象上#,c#,unity3d,C#,Unity3d,我一直在用Unity 3D制作一个游戏,其中包括能够拾取一些物体。我目前的代码如下: using System.Collections; 使用System.Collections.Generic; 使用UnityEngine public class pickUp : MonoBehaviour { public Transform dest; void OnMouseDown() { GetComponent<Rigidbody>().useGravity =

我一直在用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    
  }
}
公共类接送:单一行为
{
公共场所;
void OnMouseDown(){
GetComponent().useGravity=false;
GetComponent().velocity=新矢量3(0f,0f,0f);
GetComponent().angularVelocity=新矢量3(0f,0f,0f);
this.transform.position=dest.position;
this.transform.parent=GameObject.Find(“Destination”).transform;
}
void OnMouseUp(){
this.transform.parent=null;
GetComponent().useGravity=true;
}
无效更新(){
//我需要在哪里检测鼠标
}
}
正如评论中暗示的那样,我需要检测鼠标是否落在对象上,否则它会关闭所有其他对象的物理功能。我也无法编程该名称(因此使用Ray),因为已经有太多对象使用了该脚本

谢谢


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

我认为您缺少了一些东西,void OnMouseDown(),OnMouseOver(),OnMouseUp(),只为附加了碰撞器和脚本的特定对象调用,不管您对多个对象使用同一脚本多少次。这些方法独立于每个对象。

您最初的问题代码如下所示

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

但我需要在每一帧检测到这一点,OnMouseOver和OnMouseDown仅在它们发生时才被调用。无法在Update()中调用它们。我明白你的意思,onmouseover不正确,我正在修复(复制+粘贴旧版本)好的,我知道我做了什么。谢谢
private void OnMouseEnter()
{

}

private void OnMouseExit()
{

}