Therealmrstarfish
Therealmrstarfish

Reputation: 65

How to detect if the mouse is over the object Unity C#

I have been working on a game in Unity 3D, which involves being able to pick up some objects. My current code is as follows:

using System.Collections;

using System.Collections.Generic; using 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    
  }
}

I need to, as implied in the comment, detect if the mouse is down over the object because otherwise it turns off physics for all other objects. I also cannot program the name (so using Ray) since so many objects already use the script.

Thanks!

Edit: old version of code, put current version in

Upvotes: 2

Views: 15242

Answers (2)

derHugo
derHugo

Reputation: 90679

Your original question code looked like this

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

which was almost there but of course it makes no sense to declare this as a nested method inside of Update!

Your script should rather look like

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

Or if you need to do something only the moment the mouse enters and exists the object rather use

private void OnMouseEnter()
{

}

private void OnMouseExit()
{

}

Upvotes: 2

Baku
Baku

Reputation: 1

I think you are missing something, void OnMouseDown(), OnMouseOver(), OnMouseUp(), only called for specific object that has a collider and script attached to it, it doesn't matter how many times you use the same script for multiple objects. These methods are independent of each object.

Upvotes: 0

Related Questions