- Home /
Help with rotating a sprite with mouse.
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.
Answer by logicandchaos · Jan 18, 2020 at 02:23 AM
if you want to rotate at the point you click on you might have to set the pivot point to the mouse click location on object.
No i think you misunderstand. I want to be able to basically grab the object and rotate by spinning around it you see?
Answer by Major_Lag · Jan 19, 2020 at 06:53 AM
I made a bodge that works for me currently however Its not really future proof like for oblong objects.
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);
Vector3 dir = mousePos - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
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))
{
isBeingRotated = true;
}
}
}