Search Unity

[SOLVED] Help with Rotation Script

Discussion in 'Physics' started by Ricardo_Reis, Nov 20, 2017.

  1. Ricardo_Reis

    Ricardo_Reis

    Joined:
    Oct 15, 2017
    Posts:
    13
    Hey guys!
    So I'm building a controller and it's been working fine, today I decided to limit the Vertical rotation and I've been the past 4 hours stuck with a problem. Here's my code:

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

    public class LookY : MonoBehaviour
    {
    [SerializeField]
    private float _sensitivity = 1f;

    void Start ()
    {

    }

    void Update ()
    {
    float _mouseY = Input.GetAxis("Mouse Y");
    Vector3 newRotation = transform.localEulerAngles;
    newRotation.x -= _mouseY * _sensitivity;/*
    newRotation.x = Mathf.Clamp(newRotation.x, -80, 80);*/
    Debug.Log(newRotation.x);
    transform.localEulerAngles = newRotation;

    }
    }

    Now the line of code in comment, theoretically, should work, however there's a problem.
    For some reason the rotation I use is not the same as the rotation in the inspector, and instead of, after going up from 0 going to negative values, it goes to 360 and decreases, this obviously makes the line incorrect as going up from 0 instantly drops the x rotation.
    My question is what can I do in this situation, is there a way to use the 180 system instead of 360 or do I have to go to Quaternions? If so could anyone give me some guidance?
    Thank you all in advance!
     
  2. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    First, you'll want to use [ code ] tags around your code. That keeps formatting and makes it easier to read. :)

    Now, I think your issue is that you're wanting to use negative angles when the engine doesn't. You want to clamp the angle from 280 -> 360 -> 80, right? You could breakout your clamp into:
    Code (csharp):
    1. if (newRotation.x >= 180 && newRotation.x < 280)
    2.     newRotation.x = 280;
    3. else if (newRotation.x < 180 && newRotation.x > 80)
    4.     newRotation.x = 80;
     
    Ricardo_Reis likes this.
  3. Ricardo_Reis

    Ricardo_Reis

    Joined:
    Oct 15, 2017
    Posts:
    13
    Thank you for the tip, I did see everyone else using some way to put code that way but I couldn't find how.

    That worked!
    I actually thought of something like that, but I was under the impression that if s in Update would slow the game down considerably, but from what I've tested it doesn't seem so.
    Thank you a lot!
     
  4. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    Glad to hear it!
    "If" blocks are pretty fast by themselves. It's what you're checking that could slow things down, and comparing values directly like this is very fast. I think the logic behind Clamp would look very similar to this too, save for checking which side of 180 you're on. ;)
     
    Ricardo_Reis likes this.
  5. Ricardo_Reis

    Ricardo_Reis

    Joined:
    Oct 15, 2017
    Posts:
    13
    I always thought that checking every frame would totally kill my fps but you're right, haven't dropped much. I come from a programming background, however I was always afraid of using the basic things (ifs, fors, do whiles, etc) on Update as I thought that it'd slow the performance to a crawl, after checking that this example didn't, I'm beginning to apply this knowledge to help me with the game, including using a 'for' for (no pun intended) a smooth jump movement.
    I gotta say you opened my eyes to this, and for that that I thank you! :D