Rotation speed different in standalone build

Hello,

I’m adding rotation function to an object based on following script: Spin a 2D object with mouse drag - Questions & Answers - Unity Discussions

Here is code:

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.

Hello @AstroCool ,

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.

An alternative is to rotate your objects inside of the Monobehaviour.FixedUpdate method.

This is because depending on the CPU, in where the game or app is running, it can handle more or fewer frames per second.

I hope it helps!

Hi,

Thanks for answer. Will try after i fixed problem with Unity not working in Windows 10 fresh install.

1 Like

Hi,

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’:

rotateButton.transform.SetParent(this.transform, false);

‘ButtonMove’ is placed on Top Middle of ‘GameObjectToMove’ object with some small offset.

After some googling I found following:

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?

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.

Hi @AstroCool ,

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?

You can find more information about it here: Unity - Scripting API: Transform.RotateAround

Good luck with it!

Hi,

Thanks for suggestion. I have tried following:

this.transform.parent.transform.RotateAround(this.transform.parent.transform.position, Vector3.forward, -(vectorDiff.x + vectorDiff.y) * velocity * Time.deltaTime);

Got same effect.

I understand it something to do with ‘-(vectorDiff.x + vectorDiff.y)’ calculation that I can’t go past 180 degrees…

Hi @AstroCool ,

In that case, what you can do is to use Mathf.Clamp, in order to limit the maximum rotation to be done in a single frame.

You can find more information about such method here: Unity - Scripting API: Mathf.Clamp

Good luck with it!

Hi,

This task is closed.
I hired a freelancer to help wit this question.

2 Likes

Hi
I´ve have a similar problem

I want to shoot in direction to the mouse
In editor works well but in build it gets another offest that makes the shoots go way low than expected

@AstroCool
Can you please tell us what the freelancer did to solve this problem
I will highly appreciate it

Thanks

2 Likes