Search Unity

help me understand a code

Discussion in 'General Discussion' started by hendrickmiqueias, Jul 14, 2020.

  1. hendrickmiqueias

    hendrickmiqueias

    Joined:
    Jul 12, 2020
    Posts:
    2
    I recently wrote a code, just that it doesn't work, but the problem is that I have the same code in another script and it works, can someone tell me why?

    the code is:

    public class peito : MonoBehaviour
    {

    public float mouseY = 0.0f;
    private float sensibilidade = 2.0f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()

    {
    mouseY = Mathf.Clamp(mouseY, -30, 30);
    mouseY += Input.GetAxis("Mouse Y") * sensibilidade; <<<< this part hapen
    transform.eulerAngles = new Vector3(mouseY, 0, 0); <<<< this part don't hapen
    }

    }


    I tried to change "transform.eulerAngles" to a (declared transform).eulerAngles but don't work







    the other code that works is


    public class cabeça : MonoBehaviour
    {
    public bool travarMouse = true;
    public float sensibilidade = 2.0f;
    private float mouseX = 0.0f, mouseY = 0.0f;
    public Transform ponto;
    // Start is called before the first frame update
    void Start()
    {


    }

    // Update is called once per frame
    void Update()
    {
    mouseY = Mathf.Clamp(mouseY, -90, 80);
    mouseX += Input.GetAxis("Mouse X") * sensibilidade;
    mouseY += Input.GetAxis("Mouse Y") * sensibilidade;
    ponto.eulerAngles = new Vector3(mouseY, mouseX, 0);



    }
    }
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    First up, welcome!

    How do you know that the green line works but the red line doesn't?

    A few things to check for:
    - Is that script attached to the GameObject you think it's attached to?
    - Is it the only script attached? If not, do any other scripts also modify the eulerAngles?
    - Is sensibilidade set to a non-zero value in the Inspector?
    - Do you have any compile errors in the Console window? No changes to scripts will work until you've fixed them all.

    For future reference, this kind of question should go in the Scripting section of the forum
     
  3. hendrickmiqueias

    hendrickmiqueias

    Joined:
    Jul 12, 2020
    Posts:
    2
    Hi,
    sorry for posting in the wrong session.

    - I know that the green line works because if i change te private float to public float the value changes.
    -Is the only script attached
    - sensibilidade is set to 2f ever;
    -no have error in the console.
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I just put your exact code in a fresh Unity project, dropped it on a cube, and it's working. Moving the mouse forward and back rolls the cube.

    Screenshots of how you've got things set up would be a good next step.

    In your other piece of code you show a reference to a Transform being used to rotate something. So another thing to check is that no other GameObjects have scripts with references to the one you're using the new script on.

    I assume it's because you're debugging, but just in case... it looks to me like your "public" and "private" are being used backwards.

    Public means that other objects can change the value directly, so I wouldn't expose something like 'mouseY' to that. Private means that no other object can read or write the value, which is potentially a bit too restrictive for 'sensibilidade' (I'm guessing that roughly translates to "sensitivity" in English, ie: how much effect should mouse movements have), which I would probably also want to be visible in the Inspector.
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Try rotating the object that the script is attached to in the editor, while in play mode.

    You shouldn't be able to, since the script is settings it's rotation every frame.

    I strongly suspect that the script is working, and that red line is happening, it just isn't producing a visible effect for whatever reason (e.g. always setting rotation to 0, rotating the wrong object etc)