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);
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.
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)