Unity - Drag with right mouse button?

Viewed 3996

I am creating a build in Unity 2019.4.12f1 and need to drag an object with Right Mouse button. My C# skills are very limited, so far but i try.

This scripts is my attempt to be able to rotate a gameobject by holding right mouse button down and dragging ingame.

But it is wrong. Can anyone help me correct it?´


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();
        }
    }
}
3 Answers

I understood that You need to Drag Object in 3d World so Here is my Code just create new Script and attach to Object You Want to Drag it

Your Object that You need to Drag it should has a collider

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 try it by adding it to sphere or cube and if you use custom shape you should insure that the collider is in suitable size or has a meshcollier Demo

EDIT

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

    }
}

You could try the Input.GetMouseButton method, this should return a value depending on if the button is held pressed.

This one works too, if someone needs in the future. This is for rotate though. Sorry, i dont know why it wont let me post as code, so i post an image.

enter image description here

Related