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;
}
}
After build speed rotation is slow. Also if you drag mouse right - it somewhat rotates, to the left - almost zero. In Editor it works fine with good speed in both ways.
If you want to have the same speed on all devices (Editor and builds), you NEED to use Time.deltaTime if you want to rotate your objects in the Monobehaviour.Update method.
What I have found so far - 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 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’:
The problem is that I can’t rotate it on 360 degrees holding & following my rotate button. I can do it only 90 degrees to the left and tight (than it either begin to go reverse or juggling back and forward).
I can do full circle if I press and hold mouse button and go pointer to the right part of the screen or straight up. It will circle to the right, and in opposite direction, it will circle to the left. This is really a blocking issue as of now.
Maybe you can try to use Transform.RotateAround method instead, because from what I see in your script you’re trying to rotate your object around the z-axis, right?