I made a script that should allow the player be able to move and rotate sprites with left and right click. It does work, however, I don’t like the way it snaps to face the mouse. I want pretty much to be able to grab the object and rotate by spinning around it. What ways should i try to get this interaction? I’ve tried a few different ways to fix this but haven’t been able to understand how to make it work. Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
float startPosX;
float startPosY;
bool isBeingDragged = false;
bool isBeingRotated = false;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Debug.Log("left click up");
isBeingDragged = false;
}
if (Input.GetMouseButtonUp(1))
{
Debug.Log("right click up");
isBeingRotated = false;
}
if (isBeingDragged)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
}
else if (isBeingRotated) //if youre already being dragged you shouldnt be able to rotate.
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}
}
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
//Debug.Log("got left click");
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
startPosX = mousePos.x - this.transform.localPosition.x;
startPosY = mousePos.y - this.transform.localPosition.y;
isBeingDragged = true;
}
if (Input.GetMouseButtonDown(1))
{
//Debug.Log("got right click");
isBeingRotated = true;
}
}
}
Let me know your thoughts/improvements for any part of my script.