【问题标题】:C# OnMouseUp() not working [duplicate]C# OnMouseUp() 不工作 [重复]
【发布时间】:2018-03-20 00:11:51
【问题描述】:

我一直在搜索很多相关问题,但我仍然无法弄清楚为什么我的 OnMouseUp() 不起作用。

我想使用脚本 Mover 在通过左键单击对象选择游戏对象时将其抬起并在任意位置右键单击将其取消。

这里是游戏对象的信息。它确实有一个对撞机,所以我不知道我哪里做错了。

这是 Mover 的代码。

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

public class Mover : MonoBehaviour {

    private bool selected;

    // Use this for initialization
    void Start () {
        selected = false;
    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = GetComponent<Rigidbody>().position;
        if (Input.GetMouseButtonUp(1) && selected)
        {
            GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y - 1f, pos.z);
            selected = false;
        }
    }

    void OnMouseUp()
    {
        Vector3 pos = GetComponent<Rigidbody>().position;
        if (!selected)
        {
            GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y + 1f, pos.z);
            selected = true;
        }
        Debug.Log("OnMouseUp!");
    }
}

debug.log 也没有显示出来。

---更新---

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

public class Mover : MonoBehaviour, IPointerEnterHandler
{

    private bool selected;

    void Start () {
        selected = false;
        addPhysicsRaycaster();
    }

    void Update () {
        Vector3 pos = GetComponent<Rigidbody>().position;
        if (Input.GetMouseButtonUp(1) && selected)
        {
            GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y - 1f, pos.z);
            selected = false;
        }
    }

    void addPhysicsRaycaster()
    {
        PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();
        if (physicsRaycaster == null)
        {
            Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Vector3 pos = GetComponent<Rigidbody>().position;
        if (!selected)
        {
            GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y + 1f, pos.z);
            selected = true;
        }
        Debug.Log("OnPointerEnter!");
    }
}

【问题讨论】:

  • 不要使用任何OnMouseXXX 函数。使用OnPointerEnter。参见副本中的#6。
  • @Programmer 您好,我尝试了 OnPointerEnter 和 OnPointerDown,但仍然无法正常工作,您能否检查一下我在更新区域中的代码?
  • 您的代码现在看起来不错。您是否将PhysicsRaycaster 附加到相机?你必须这样做!请参阅副本以了解如何从代码中执行此操作
  • 只需将图形光线投射器添加到相机中,没有很好的文档记录,但需要该组件才能将 3d 世界与事件系统联系起来(您还需要在场景中使用事件系统)
  • @Programmer 非常感谢你这么耐心地教我!

标签: c# unity3d


【解决方案1】:

检查Mesh collider中的'Is Trigger',同时确保你的collider在那里,如果仍然不起作用,你可以尝试不同的collider(例如box collider)。

对属于 Ignore Raycast 层的对象不调用此函数。

当且仅当在标记为触发器的碰撞器上调用此函数 Physics.queriesHitTriggers 为真。

请了解更多关于统一文档https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUp.html

【讨论】:

  • 您好,我之前尝试过检查“Is Trigger”,但没有成功。游戏对象是一个圆柱体。你还推荐使用盒子碰撞器吗?
  • 应该可以加一个mesh colider。
  • @AflahBhari 嗨,您知道为什么它不起作用吗?是我使用方式不对吗?
  • @SeakyLone 不是很不幸。我会尝试将 Physics.queriesHitTriggers 设置为 true 并查看它的作用。编辑:对不起,你可能想做这样的事情:docs.unity3d.com/ScriptReference/QueryTriggerInteraction.html
  • @AflahBhari,你是对的,除非你设置了 QueryTriggerInteraction,否则没有必要。如果不了解现场情况,很难知道确切的问题是什么。还要确保对撞机没有阻挡物体
猜你喜欢
  • 2011-09-23
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2015-04-21
  • 2017-12-12
  • 2012-11-15
  • 2018-03-29
相关资源
最近更新 更多