Pastebin Search

Untitled

Tue May 05 2020
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class Rotate : EventTrigger
{
    //public Transform cube;

    float deltaRotation;
    float previousRotation;
    float currentRotation;
    float speed = 0.8f;
    private bool dragging;
    public GameObject pressedButton;

    void Start()
    {

    }

    void Update()
    {
        if (dragging)
        {
            //if (Input.GetMouseButtonDown(0))
            //{
            //    deltaRotation = 0f;
            //    previousRotation = angleBetweenPoints(pressedButton.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
            //}
            //else if (Input.GetMouseButton(0))
            //{
            currentRotation = angleBetweenPoints(pressedButton.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
            deltaRotation = Mathf.DeltaAngle(currentRotation, previousRotation);
            previousRotation = currentRotation;
            pressedButton.transform.Rotate(Vector3.back, deltaRotation);

            //Inverse rotation for build:
            //pressedButton.transform.rotation = Quaternion.Inverse(pressedButton.transform.rotation);

            //}
        }

    }

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

        cross.z *= Time.deltaTime * -10f; //To adjust for speed between different hardware?
        //cross.z *= -1; //inverse ?

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