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

Help with a camera rotation code(it doesn't rotate left)

Discussion in 'Scripting' started by unity_ATLH5prSV7NlLQ, Feb 23, 2022.

  1. unity_ATLH5prSV7NlLQ

    unity_ATLH5prSV7NlLQ

    Joined:
    Feb 23, 2022
    Posts:
    2
    public float RotationSpeed = 15f;
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    if (Input.mousePosition.x >= Screen.width && Input.mousePosition.x > 0)
    {
    transform.Rotate(Vector3.up, RotationSpeed * Time.deltaTime);
    }
    else if (Input.mousePosition.x >= Screen.width && Input.mousePosition.x < 0)
    {
    transform.Rotate(Vector3.down, RotationSpeed * Time.deltaTime);
    }
    }
     
  2. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    82
    Likely something inside your if statement isn't matching how you'd expect. You should use a debug in there to see what the ACTUAL values of Input.mousePosition.x and Screen.width are, so you can manually compare the numbers and see why the statements aren't working the way you think it should be.
    But, I don't see how your X mouse position can be less than zero while also being greater than or equal to the width of the screen. . . such as what you're asking for in your else if statement.
     
  3. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    .up & .down are for directions...
    you maybe need to Negate the axis

    Code (CSharp):
    1. transform.Rotate(-Vector3.up, RotationSpeed * Time.deltaTime);
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This ^ ^ ^

    Your if statements are asking if the mouse is greater than the width of the screen.

    It's also asking if it's less than the left edge of the screen.

    Both of those things seem a) unlikely, or b) if they happened, it would be totally dependent on window size.

    Are you instead looking to see if the mouse is on the left or the right half of the screen? If so, then divide the width by two and check against that.

    Either way, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, consider using Debug.DrawRay() or Debug.DrawLine() to visualize things like raycasts or distances.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494