Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotation speed different in standalone build

Discussion in 'Editor & General Support' started by AstroCool, May 5, 2020.

  1. AstroCool

    AstroCool

    Joined:
    Jul 17, 2018
    Posts:
    36
    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

    Here is code:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class Rotate : EventTrigger
    5. {
    6.     float deltaRotation;
    7.     float previousRotation;
    8.     float currentRotation;
    9.     //float speed = 0.8f;
    10.     private bool dragging;
    11.     public GameObject pressedButton;
    12.     //float rotateSpeed = 100f;
    13.     /*    void Start()
    14.     {
    15.  
    16.     }*/
    17.  
    18.     void Update()
    19.     {
    20.         if (dragging)
    21.         {
    22.             currentRotation = angleBetweenPoints(pressedButton.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
    23.             deltaRotation = Mathf.DeltaAngle(currentRotation, previousRotation);
    24.             previousRotation = currentRotation;
    25.             pressedButton.transform.Rotate(Vector3.back, deltaRotation); // * Time.deltaTime
    26.         }
    27.  
    28.     }
    29.  
    30.     float angleBetweenPoints(Vector2 position1, Vector2 position2)
    31.     {
    32.         var fromLine = position2 - position1;
    33.         var toLine = new Vector2(1, 0);
    34.         var angle = Vector2.Angle(fromLine, toLine);
    35.         var cross = Vector3.Cross(fromLine, toLine);
    36.  
    37.         // did we wrap around?
    38.         if (cross.z > 0)
    39.             angle = 360f - angle;
    40.  
    41.         return angle;
    42.     }
    43.  
    44.     public override void OnPointerDown(PointerEventData eventData)
    45.     {
    46.         dragging = true;
    47.  
    48.         deltaRotation = 0f;
    49.         previousRotation = angleBetweenPoints(pressedButton.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
    50.  
    51.     }
    52.  
    53.     public override void OnPointerUp(PointerEventData eventData)
    54.     {
    55.         dragging = false;
    56.     }
    57. }
    58.  
    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.
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    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!
     
  3. AstroCool

    AstroCool

    Joined:
    Jul 17, 2018
    Posts:
    36
    Hi,

    Thanks for answer. Will try after i fixed problem with Unity not working in Windows 10 fresh install.
     
    DiegoDePalacio likes this.
  4. AstroCool

    AstroCool

    Joined:
    Jul 17, 2018
    Posts:
    36
    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':
    Code (CSharp):
    1. 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?
     
  5. AstroCool

    AstroCool

    Joined:
    Jul 17, 2018
    Posts:
    36
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class eRotateObj : EventTrigger
    7. {
    8.     private bool _isRotating;
    9.     private Vector3 startMousePos;
    10.     private Vector3 currentMousePos;
    11.     private float velocity = 2f;
    12.     Quaternion originRotation;
    13.     float updatedAngleHorizontal;
    14.     float updatedAngleVertical;
    15.     float startAngleHorizontal;
    16.     float startAngleVertical;
    17.     float angleDiffX;
    18.     float angleDiffY;
    19.     Vector3 rotateTo = Vector3.zero;
    20.  
    21.     void Update()
    22.     {
    23.         if (_isRotating)
    24.         {
    25.  
    26.             Debug.Log("---------");
    27.  
    28.             currentMousePos = Camera.main.WorldToScreenPoint(Input.mousePosition);   //get current position.
    29.  
    30.             Debug.Log("currentMousePos= " + currentMousePos);
    31.  
    32.             var vectorDiff = currentMousePos - startMousePos;
    33.  
    34.             rotateTo.z = -(vectorDiff.x + vectorDiff.y) * velocity;
    35.  
    36.  
    37.             this.transform.parent.transform.Rotate(rotateTo);
    38.  
    39.             Debug.Log("vectorDiff= " + vectorDiff);
    40.  
    41.             //vectorDiff = Vector3.zero;
    42.             //rotateTo = Vector3.zero;
    43.  
    44.             startMousePos = Camera.main.WorldToScreenPoint(Input.mousePosition);
    45.         }
    46.     }
    47.  
    48.     public override void OnPointerDown(PointerEventData eventData)
    49.     {
    50.         // rotating flag
    51.         _isRotating = true;
    52.  
    53.         originRotation = this.transform.parent.transform.rotation;
    54.  
    55.         startMousePos = Camera.main.WorldToScreenPoint(Input.mousePosition); //get start position.
    56.  
    57.         Debug.Log("startMousePos= " + startMousePos);
    58.  
    59.     }
    60.  
    61.     public override void OnPointerUp(PointerEventData eventData)
    62.     {
    63.         // rotating flag
    64.         _isRotating = false;
    65.     }
    66. }
    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.
     
  6. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
  7. AstroCool

    AstroCool

    Joined:
    Jul 17, 2018
    Posts:
    36
    Hi,

    Thanks for suggestion. I have tried following:


    Code (CSharp):
    1. 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...
     
  8. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
  9. AstroCool

    AstroCool

    Joined:
    Jul 17, 2018
    Posts:
    36
    Hi,

    This task is closed.
    I hired a freelancer to help wit this question.
     
    DiegoDePalacio likes this.
  10. AAManiqueH

    AAManiqueH

    Joined:
    Apr 29, 2020
    Posts:
    3
    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
     
    jollins likes this.