C#OnMouseUp()无法正常工作 [英] C# OnMouseUp() not working

查看:210
本文介绍了C#OnMouseUp()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索许多相关问题,但是我仍然无法弄清为什么我的OnMouseUp()无法正常工作.

I have been searching a lot of related questions but I still couldn't figure out why my OnMouseUp() is not working.

我想使用脚本移动器在左击鼠标对象时将其抬高,然后在任意位置单击鼠标右键将其取消.

I want to use the script Mover to raise the gameobject up when it is selected with a left click on the object and cancel it with a right click anywhere.

这是游戏对象的信息.它确实有一个对撞机,所以我不知道我做错了什么地方.

Here is the information of the gameobject. It does have a collider, so I don't know where I did wrong.

这是Mover的代码.

Here is the code of 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也没有显示.

The debug.log isn't showing up as well.

-更新--

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!");
    }
}

推荐答案

在Mesh碰撞器中检查'Is Trigger',还要确保您的碰撞器在那里,如果仍然无法使用,可以尝试使用其他碰撞器(例如Box Collider)

Check 'Is Trigger' in the Mesh collider, also make sure your collider is there, you can try different collider (e.g box collider) if it's still not work.

在属于忽略Raycast"层的对象上不会调用此函数.

This function is not called on objects that belong to Ignore Raycast layer.

当且仅当以下情况下,在标记为触发器"的碰撞器上调用此函数 Physics.queriesHitTriggers是正确的.

This function is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true.

请详细了解统一文档 https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUp.html

这篇关于C#OnMouseUp()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆