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