0

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.

enter image description here

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

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

---Update---

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!");
    }
}
Seaky Lone
  • 992
  • 1
  • 10
  • 29
  • Do not use any of the `OnMouseXXX` functions. Use `OnPointerEnter`. See #6 from the duplicate. – Programmer Mar 19 '18 at 02:00
  • @Programmer Hi, I tried both OnPointerEnter and OnPointerDown but it still doesn't work, Could you please check my code in the update region? – Seaky Lone Mar 19 '18 at 02:14
  • Your code seems fine now. Did you attach `PhysicsRaycaster` to the camera? You must do that! See the duplicate for how to do that from code too – Programmer Mar 19 '18 at 05:18
  • Just add a graphics raycaster to the camera, is not documented very well but that component is required to tie 3d world with event system (you will also need an event system on scene) – zambari Mar 19 '18 at 09:44
  • @zambari Really bad advice. That's not required and `GraphicRaycaster` is used to raycast UI elements. Also, `GraphicRaycaster` is supposed to be attached to the `Canvas` not the camera but this has nothing to do with the question because OP wants to detect the click event on a 3D mesh not UI element. – Programmer Mar 19 '18 at 14:56
  • @Programmer Hi, I have updated the code of my main camera. Could you please check it out? – Seaky Lone Mar 19 '18 at 15:46
  • `MeshDetector` should be attached to the Object with the collider that you want to detect the clicks on. In this case,it is the "Piece" Object not the camera. The `PhysicsRaycaster` is attached to the camera. The `addPhysicsRaycaster` will attach `PhysicsRaycaster` to the main camera for you during run/play-time. By the the way, why can't you just copy the code from the duplicate directly to save time? I noticed you didn't because it should be `void OnPointerEnter` not `void IPointerEnterHandler.OnPointerEnter`. You don't have to add ---Update3--- just keep updating the first update. – Programmer Mar 19 '18 at 15:55
  • @Programmer Do you mean I should attach those to my Mover script? I used IPointerEnterHandler.OnPointerEnter because the Visual Studio says OnPointerEnter is not correct. – Seaky Lone Mar 19 '18 at 16:13
  • You get error because you didn't include `using UnityEngine.EventSystems;` at the top. Include that too. You don't attach scripts to scripts. You attach script to GamObject. In your screenshot, "Piece" is the GameObject so attach the `Mover` to the "Piece" GameObject. If it's not working, remove non useful stuff from your project, zip and upload it somewhere then link it here and I will take a look. Just leave what's required to replicate the issue which is the model – Programmer Mar 19 '18 at 17:01
  • @Programmer Please check the War Song.rar. in the link: https://github.com/SeakyLuo/Win-Mac BTW, I have renamed Mover to MeshDetector and I also used a similar algorithm in GameController, which is attached to the Board, to detect the mouse click. But still not working. – Seaky Lone Mar 19 '18 at 17:13
  • You can delete it now. I found two issues: **1.** The Piece GameObject is too small. That's because the y(Scale) axis is set to `0.3` while the x and z are set to `1.5`.This confuses Unity physics. Simply change the y(Scale) to also `1.5`. **2.** You are missing `EventSystem` in your scene. That should be automatically created for you when you create a UI Object but you have not created UI Object so it's not there. Manually create `EventSystem` for the scene. Go to GameObject--->UI---Event System. If you click on the Piece now, the detection should work fine. Let me know. – Programmer Mar 19 '18 at 17:51
  • @Programmer Thank you so much! It works now! I have another simple question. The real height of the piece (Since I want to immitate a real chess game) is actually that small. Is there a way to resolve this? – Seaky Lone Mar 19 '18 at 18:00
  • Yes. 3 ways to resolve this. The object Scale must be set to certain size to be detect so leave that fix alone. [1].If you want the size to be smaller, move the object (position) back or [2] move the camera (position) back. It's usually the Z(position) axis that you move but your project setup is messed up. For your project, it's the Y(position) axis. [3] If that's not giving you the effect you want, you can simply move the **Field of View** of the camera. That should work too without moving anything. – Programmer Mar 19 '18 at 18:08
  • 1
    @Programmer Thank you so much for teaching me so patiently! – Seaky Lone Mar 20 '18 at 01:52

1 Answers1

0

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.

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

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

Please learn more on unity documentation https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUp.html

endrik exe
  • 606
  • 3
  • 10
  • Hi I tried checking the “Is Trigger” before but it didn't work. And the gameobject is a cylinder. Do you still recommend using a box collider? – Seaky Lone Mar 19 '18 at 01:52
  • Should be able to add a mesh colider. – Aflah Bhari Mar 19 '18 at 01:56
  • @AflahBhari Hi, do you have any idea why it's not working? Is the way I use it wrong? – Seaky Lone Mar 19 '18 at 01:57
  • @SeakyLone Not really unfortunately. I'd try setting Physics.queriesHitTriggers to true and seeing what that does. Edit: sorry you'd probably want to do something like this: https://docs.unity3d.com/ScriptReference/QueryTriggerInteraction.html – Aflah Bhari Mar 19 '18 at 01:59
  • @AflahBhari, you are right it's not necesarry unless you set QueryTriggerInteraction. it's hard to know what the exact problem is without knowing the scene condition. Also make sure there's no blocking object to your collider – endrik exe Mar 19 '18 at 02:08
  • @endrikexe I just started to build my game. So I only have a plane and that cylinder on it. Do you want me to post the scene? – Seaky Lone Mar 19 '18 at 02:27