Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drag with right mouse button?

I am creating a build in Unity 2019.4.12f1 and need to drag an object with Right Mouse button.

This script is my attempt to be able to rotate a game object by holding the right mouse button down and dragging in-game.

But it is wrong.


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

public class mouseM : MonoBehaviour
{
     bool dragging = false;

     void Start()
    {
        if (Input.GetMouseButtonDown(1))
        {
            dragging = true;
        }
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            yourOnMouseDownFunction();
            dragging = true;
        }
        if (Input.GetMouseButtonUp(1))
        {
            yourOnMouseUpFunction();
            dragging = false;
        }

        if (dragging)
        {
            yourOnMouseDragFunction();
        }
    }
}
like image 321
Jesper Green Jensen Avatar asked Oct 16 '25 20:10

Jesper Green Jensen


1 Answers

Just create a new script and attach it to the object you want to drag. The object should have a collider.

If you just want to drag with a left mouse button:

using UnityEngine;
public class DragableObject : MonoBehaviour
{
    private Vector3 mOffset;
    private float mZCoord;

    private void OnMouseDrag()
    {
        transform.position = GetMouseWorldPos() + mOffset;
    }

    private void OnMouseDown()
    {
        mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
        mOffset = transform.position - GetMouseWorldPos();
    }

    private Vector3 GetMouseWorldPos()
    {
        Vector3 mosePoint = Input.mousePosition;
        mosePoint.z = mZCoord;
        var result = Camera.main.ScreenToWorldPoint(mosePoint);
        return result;
    }
}

You can also use this idea with Unity Event System to do same operation but with a right mouse button.

You can try it by adding it to a sphere or cube and if you use a custom shape you should insure that the collider is in a suitable size or has a meshcollier.

Demo

And if you want to use the right mouse button to drag, you can use this script too:

using UnityEngine;
public class DragableObject : MonoBehaviour
{
    private bool isMouseDragging;
    private Vector3 screenPosition;
    private Vector3 offset;
    private GameObject target;

    GameObject ReturnClickedObject(out RaycastHit hit)
    {
        GameObject targetObject = null;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
        {
            targetObject = hit.collider.gameObject;
        }
        return targetObject;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit hitInfo;
            target = ReturnClickedObject(out hitInfo);
            if (target != null)
            {
                    isMouseDragging = true;
                    Debug.Log("our target position :" + target.transform.position);
                    //Here we Convert world position to screen position.
                    screenPosition = Camera.main.WorldToScreenPoint(target.transform.position);
                offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
            }
        }

        if (Input.GetMouseButtonUp(1))
        {
            isMouseDragging = false;
        }

        if (isMouseDragging)
        {
            //tracking mouse position.
            Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);

            //convert screen position to world position with offset changes.
            Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;

            //It will update target gameobject's current postion.
            target.transform.position = currentPosition;
        }

    }
}
like image 115
Hassan Omar Avatar answered Oct 18 '25 20:10

Hassan Omar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!