1
\$\begingroup\$

I have this object. I want to click on it and drag it around. Also if I click anywhere on the screen, I want the object to move but keep the distance.

In the script below I made the object follow the mouse, but I can't do the second part.

I have a couple rays for debugging. Any ideas?

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

public class Player : MonoBehaviour
{
    Vector3 playerPosition;
    Vector3 mouseF;
    Vector3 mouseN;

    void FixedUpdate()
    {
        if (Input.GetMouseButton(0))
        {
            setMouseF();
            setMouseN();

            Debug.DrawRay(mouseN, mouseF - mouseN, Color.green);
            Debug.DrawRay(mouseN, (playerPosition - mouseN), Color.red);

            // move the player base on mouse position
            transform.position = mouseF;
        }

        if (Input.GetMouseButtonUp(0))
        {

            // on mouse release, set the new player position
            playerPosition = mouseF;
        }

    }

    void setMouseF()
    {
        Vector3 mouseFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
        mouseF = Camera.main.ScreenToWorldPoint(mouseFar);
    }

    void setMouseN()
    {
        Vector3 mouseNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
        mouseN = Camera.main.ScreenToWorldPoint(mouseNear);
    }
}

enter image description here

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "keeping the distance" to what? What's the expected/desired result if I click at the position of the right-hand orange arrow in your image? Where should the white cube go/how should it move in response? \$\endgroup\$
    – DMGregory
    Mar 13, 2019 at 0:20

2 Answers 2

0
\$\begingroup\$

You want to drag & drop an object and keep the distance between the mouse and the object is that right?

If this is what you want it might be something like

Vector3 initialMousePos;
Vector3 offset;

private void Update() {
    if(Input.GetMouseButtonDown(0))
    {
        initialMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }
    if(Input.GetMouseButton(0))
    {
        offset = initialMousePos - Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position += offset;
    }
}

Tell me if that helps.

\$\endgroup\$
7
  • \$\begingroup\$ Kind of, see my updated post, I added an image \$\endgroup\$ Mar 10, 2019 at 22:52
  • \$\begingroup\$ Well, I think my solution might work in that case, have you tried? \$\endgroup\$ Mar 11, 2019 at 15:03
  • \$\begingroup\$ I have, it doesn't. \$\endgroup\$ Mar 11, 2019 at 19:13
  • \$\begingroup\$ @Patrioticcow generally "it doesn't work" isn't enough information to solve the problem. Be sure to include more constructive feedback like "When I implement the solution like X, the result I observe is Y, and this differs in fashion Z from what I want it to do" \$\endgroup\$
    – DMGregory
    Mar 13, 2019 at 0:19
  • \$\begingroup\$ @DMGregory the above answer doesn't even get close to what I want. It just doesn't work. The idea is simple. Drag the object around, from it's position, no matter where I click on the screen, relative to my mouse. \$\endgroup\$ Mar 13, 2019 at 2:47
0
\$\begingroup\$

After playing some more with it, I sound a solution.

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

public class Player : MonoBehaviour
{
    Vector3 playerPosition;
    Vector3 mouseF;
    Vector3 mouseN;
    Vector3 diff;

    void FixedUpdate()
    {
        if (Input.GetMouseButtonDown(0))
        {
            InitPlayer();
        }

        if (Input.GetMouseButton(0))
        {
            SetMouseF();
            SetMouseN();

            Debug.DrawRay(mouseN, mouseF - mouseN, Color.green);
            Debug.DrawRay(mouseN, (mouseF + diff) - mouseN, Color.red);

            // move the player base on mouse position
            transform.position = mouseF + diff;
        }

        if (Input.GetMouseButtonUp(0))
        {
        }

    }

    void InitPlayer()
    {
        SetMouseF();

        // set player position where the object is
        playerPosition = transform.position;

        // get the distance between the player and mouse click
        diff = playerPosition - mouseF;
    }

    void SetMouseF()
    {
        Vector3 mouseFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
        mouseF = Camera.main.ScreenToWorldPoint(mouseFar);
    }

    void SetMouseN()
    {
        Vector3 mouseNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
        mouseN = Camera.main.ScreenToWorldPoint(mouseNear);
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .