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

Camera rotation breaks Unity

Discussion in 'Scripting' started by filipetakanap, Nov 22, 2021.

  1. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    I have the following code for a button:

    Code (CSharp):
    1.  public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         //rotate = true;
    4.         while (Input.GetMouseButtonDown(0))
    5.         {
    6.              speed = speed * 2;
    7.             Camera.main.transform.RotateAround(Camera.main.transform.position, Vector3.up, speed);
    8.             // throw new System.NotImplementedException();
    9.             Debug.Log("Estou a clicar");
    10.         }
    11.      
    12.     }
    13.  
    14.     public void OnPointerUp(PointerEventData eventData)
    15.     {
    16.         //rotate = false;
    17.         //throw new System.NotImplementedException();
    18.     }
    The intented porpose for it is, when I click it, it rotates de main camera, instead it crashes unity leaving it unresponsive until I terminate it with high usage of ram.

    Sorry if there is something missing, I'm new in the whole Unity/3D thing and i've tried different methods today, that is why the comments.

    EDIT: Just tried a different method:
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         while (Input.GetMouseButtonDown(0))
    4.         {
    5.             //speed = speed * 2;
    6.             Camera.main.transform.RotateAround(Camera.main.transform.position, Vector3.up, 2f * Time.deltaTime);
    7.             // throw new System.NotImplementedException();
    8.             Debug.Log("Estou a clicar");
    9.         }
    10.     }
    with this script on the button, but still crashes unity
     
    Last edited: Nov 22, 2021
  2. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    The reason is the while loop. Unity updates the state of the control between frames, so the while condition will not change inside the "Update" method.

    You should change the while statement to an if statement.

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (Input.GetMouseButtonDown(0)) {
    4.         Camera.main.transform.RotateAround(Camera.main.transform.position, Vector3.up, 2f * Time.deltaTime);
    5.         Debug.Log("Estou a clicar");
    6.     }
    7. }
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    It's also not described as a crash, it's an infinite loop (lock) on the main-thread so until you exit, nothing else can perform work. It's continuing to do exactly what you told it to perfectly fine, just forever! :)

    So you know, a crash would result in a crash window that included the state of the Editor (etc) that allows you to report a bug.
     
  4. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Thank you so much for the replys!

    I've tried that aswel, but it just rotates the camera once, that is why I tried to use the "while" GetMouseButtonDown(0)
    to keep rotating while the button is pressed, im not sure what to do :(

    EDIT: Stupid me. It's not "GetMouseButtonDown", but "GetMouseButton" so it keeps repeating the if statement..
    New problem: If I create a script to rotate the other way around in another button, it doesn't rotate whatsoever...

    public class rightClick : MonoBehaviour
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButton(0))
    4.         {
    5.             Camera.main.transform.RotateAround(Camera.main.transform.position, Vector3.down, 25f * Time.deltaTime);
    6.             Debug.Log("Estou a clicar");
    7.         }
    8.     }
    public class leftClick : MonoBehaviour
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButton(0))
    4.         {
    5.             Camera.main.transform.RotateAround(Camera.main.transform.position, Vector3.up, 25f * Time.deltaTime);
    6.             Debug.Log("Estou a clicar");
    7.         }
    8.     }
    I think the easiest fix would be something like "if (cursor.hover.button)"?
    As I understand the update method on a button script works for the whole app... or am I doing something wrong? In this case both updates work when i click anywhere and one is conteracting the other.
     
    Last edited: Nov 23, 2021
    ubbelito likes this.
  5. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    I finally figured it out, what a brain teaser.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5. using UnityEngine.EventSystems;
    6. using UnityEngine.UI;
    7.  
    8. public class buttonsScript : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    9. {
    10.     public UnityEvent onLongClick;
    11.     public bool rotate;
    12.  
    13.     public float speed = 25f;
    14.     // Start is called before the first frame update
    15.  
    16.     public void OnPointerDown(PointerEventData eventData)
    17.     {
    18.         rotate = true;
    19.      
    20.      
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.        if (rotate == true)
    26.         {
    27.             Camera.main.transform.RotateAround(Camera.main.transform.position, Vector3.up, 35f * Time.deltaTime);
    28.         }
    29.     }
    30.  
    31.     public void OnPointerUp(PointerEventData eventData)
    32.     {
    33.         rotate = false;
    34.         //throw new System.NotImplementedException();
    35.     }
    36.  
    37.  
    38. }
    39.  
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    You know, as you scale-up your game, you'll realised that you're doing bad things here. You're essentially searching for the main camera then looking for its transform. You should always store components in a temporary variable if you're going to use it multiple times. You can and should store it at the start permanently to be used by your script if you're not expecting the main camera to change just like you see devs using "GetComponent<XXX>" in the Start method.
     
    filipetakanap likes this.