Hello,
I'm adding rotation function to an object based on following script: https://answers.unity.com/questions/716086/spin-a-2d-object-with-mouse-drag.html
using UnityEngine;
using UnityEngine.EventSystems;
public class Rotate : EventTrigger
{
float deltaRotation;
float previousRotation;
float currentRotation;
//float speed = 0.8f;
private bool dragging;
public GameObject pressedButton;
//float rotateSpeed = 100f;
/* void Start()
{
}*/
void Update()
{
if (dragging)
{
currentRotation = angleBetweenPoints(pressedButton.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
deltaRotation = Mathf.DeltaAngle(currentRotation, previousRotation);
previousRotation = currentRotation;
pressedButton.transform.Rotate(Vector3.back, deltaRotation); // * Time.deltaTime
}
}
float angleBetweenPoints(Vector2 position1, Vector2 position2)
{
var fromLine = position2 - position1;
var toLine = new Vector2(1, 0);
var angle = Vector2.Angle(fromLine, toLine);
var cross = Vector3.Cross(fromLine, toLine);
// did we wrap around?
if (cross.z > 0)
angle = 360f - angle;
return angle;
}
public override void OnPointerDown(PointerEventData eventData)
{
dragging = true;
deltaRotation = 0f;
previousRotation = angleBetweenPoints(pressedButton.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
public override void OnPointerUp(PointerEventData eventData)
{
dragging = false;
}
}
What I have now is Editor mouse move and build mouse move are inverted. In editor when i move slowly left objects moves with it. In Build when i move in same manner to the left it moves to the right.
Currently I have following structure:
GameObjectToMove
ButtonMove
OtherButtons
Rotation script is attach to 'ButtonMove' and 'GameObjectToMove' are passed in the script (another variant is to access it through transform.parent, result is the same). The parent connection is done by following command in script that is attached to 'GameObjectToMove':
rotateButton.transform.SetParent(this.transform, false);
'ButtonMove' is placed on the Top, Middle of 'GameObjectToMove' object with some small offset.
After some googling I found following:
https://answers.unity.com/questions/1468279/screentoworldpoint-and-mouse-position.html
ScreenSpace has it's origin at the bottom left of the screen while GUI space has it's origin at the top left.
Input.mousePosition returns the mouse position in ScreenSpace.
The Event class gives the mouse position in GUI space.
ScreenToWorldPoint expects the input point in ScreenSpace.
So I think the GUI & ScreenSpace difference can cause the varied behavior as I use ScreenToWorldPoint and Event class.
Another question if Anchors plays any role in this situation?