Mathf.Clamp don't work

I’m making a Mouse Look script, here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class _MouseLook : MonoBehaviour
{
public Vector2 turn;
public float sensitivity = .5f;
public Vector3 deltaMove;
public float speed = 1;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
Mathf.Clamp(0f,-90f,0);
Mathf.Clamp(0f, 90f,0);

turn.x += Input.GetAxis(“Mouse X”) * sensitivity;
turn.y += Input.GetAxis(“Mouse Y”) * sensitivity;
transform.localRotation = Quaternion.Euler(-turn.y, turn.x, 0);

}
}

I added 2 functions for clamping and it don’t work, if you got an answer to why this is happening, please tell mee.

Anytime you say “XXX doesn’t work” and that XXX is something in a huge API such as Unity or Windows or whatever you’re programming, that should set off a little voice in your head.

“Hmm, maybe I’m not using it properly. What does the documentation say?”

If you did this you would see you are not using it correctly.

And lucky you, the documentation even includes sample code!!!

2 Likes

this might not work but you could try this

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

public class _MouseLook : MonoBehaviour
{
public Vector2 turn;
public float sensitivity = .5f;
public Vector3 deltaMove;
public float speed = 1;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float turnX += Input.GetAxis(“Mouse X”) * sensitivity;
float turnY += Input.GetAxis(“Mouse Y”) * sensitivity;
float xRotation -= turnY;

Vector3 turn = new Vector3(xRotation, turnX, 0f);
turn.x = Mathf.Clamp(turn.x, -90, 90);

transform.localRotation = Quaternion.Euler(turn.x, turn.y, 0f);

}
}

So you know, here’s how to post code on the forums: Using code tags properly

2 Likes

It’s not hard to learn how to format your code for the forum, even when you’re coming out of nowhere and necro-posting to long-ago closed threads.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

1 Like