Rotation speed different in standalone build

Started by
1 comment, last by AstroCool 4 years ago

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?

Advertisement

Hi,

Two weeks and still no result I'm looking for. I have re-done script myself and still getting problems.

My current script is following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class eRotateObj : EventTrigger
{
    private bool _isRotating;
    private Vector3 startMousePos;
    private Vector3 currentMousePos;
    private float velocity = 2f;
    Quaternion originRotation;
    float updatedAngleHorizontal;
    float updatedAngleVertical;
    float startAngleHorizontal;
    float startAngleVertical;
    float angleDiffX;
    float angleDiffY;
    Vector3 rotateTo = Vector3.zero;

    void Update()
    {
        if (_isRotating)
        {

            Debug.Log("---------");

            currentMousePos = Camera.main.WorldToScreenPoint(Input.mousePosition);   //get current position.

            Debug.Log("currentMousePos= " + currentMousePos);

            var vectorDiff = currentMousePos - startMousePos;

            rotateTo.z = -(vectorDiff.x + vectorDiff.y) * velocity;


            this.transform.parent.transform.Rotate(rotateTo);

            Debug.Log("vectorDiff= " + vectorDiff);

            //vectorDiff = Vector3.zero;
            //rotateTo = Vector3.zero;

            startMousePos = Camera.main.WorldToScreenPoint(Input.mousePosition);
        }
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        // rotating flag
        _isRotating = true;

        originRotation = this.transform.parent.transform.rotation;

        startMousePos = Camera.main.WorldToScreenPoint(Input.mousePosition); //get start position.

        Debug.Log("startMousePos= " + startMousePos);

    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        // rotating flag
        _isRotating = false;
    }
}

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.

Would appreciate any help.

Thanks.

This topic is closed to new replies.

Advertisement