Hello! I'm trying to clamp only my x rotation between -90 and 90 degrees using: private float xRotation = 0.0f; void Update() { xRotation = Mathf.Clamp(xRotation, -90, 90); transform.eulerAngles = new Vector3(xRotation, 0.0f, 0.0f); } However, the result of this is that my character can only rotate -1 to 1 on all axis. I don't understand why this is working like that. Iv'e been looking through all code samples and trying things for 2 hours now. Any help is very much appreciated
Your code is very confusing..You are setting your xRotation to = 0... Then you are saying that your xRotation is = the Constraints of itself, -90, 90... Here is a code i have for my game.. Its a touch to rotate game.. Using ray casts and nav mesh i move my player so he doesn't rotate by touch, only by the given path, but this is attached to my camera and i can rotate my camera based on the min and max values i set it too.. Min and max being up and down and able to turn a full 360.. You can hopefully use it and change it to your needs.. GoodLuck. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")] public class InputManager : MonoBehaviour { public Transform target; public float distance = 5.0f; public float xSpeed = 120.0f; public float ySpeed = 120.0f; public float yMinLimit = -20f; public float yMaxLimit = 80f; public float distanceMin = .5f; public float distanceMax = 15f; public float smoothTime = 2f; float rotationYAxis = 0.0f; float rotationXAxis = 0.0f; float velocityX = 0.0f; float velocityY = 0.0f; // Use this for initialization void Start() { Vector3 angles = transform.eulerAngles; rotationYAxis = angles.y; rotationXAxis = angles.x; // Make the rigid body not change rotation if (GetComponent<Rigidbody>()) { GetComponent<Rigidbody>().freezeRotation = true; } } void ZoomOrthoCamera(Vector3 zoomTowards, float amount) { // Calculate how much we will have to move towards the zoomTowards position float multiplier = (1.0f / this.GetComponent<Camera>().orthographicSize * amount); // Move camera transform.position += (zoomTowards - transform.position) * multiplier; // Zoom camera this.GetComponent<Camera>().orthographicSize -= amount; // Limit zoom this.GetComponent<Camera>().orthographicSize = Mathf.Clamp(this.GetComponent<Camera>().orthographicSize, distanceMin,distanceMax); } void LateUpdate() { if (Input.GetAxis ("Mouse ScrollWheel") > 0) { ZoomOrthoCamera (Camera.main.ScreenToWorldPoint (Input.mousePosition), 1); } if (Input.GetAxis ("Mouse ScrollWheel") < 0) { ZoomOrthoCamera (Camera.main.ScreenToWorldPoint (Input.mousePosition),-1); } if (target) { if (Input.GetMouseButton(0)) { velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f; velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f; } rotationYAxis += velocityX; rotationXAxis -= velocityY; rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit); Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0); Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0); Quaternion rotation = toRotation; //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax); //RaycastHit hit; //if (Physics.Linecast(target.position, transform.position, out hit)) //{ // distance -= hit.distance; //} Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = rotation * negDistance + target.position; transform.rotation = rotation; transform.position = position; velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime); velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime); } } public static float ClampAngle(float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp(angle, min, max); } }
Or use this piece here.. Code (CSharp): public static float ClampAngle(float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp(angle, min, max); } Angle beingyour xRotation
Thanks for trying to help. I winded up using this: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 6.0F; public float baseSpeed; public float rotateSpeed = 3.0F; private Vector3 moveDirection = Vector3.zero; private float rotatingStart; private float rotatingUpdate; float rotationX = 0; float rotationY = 0; float minRotationX = -45; float maxRotationX = 45; void Update() { rotationX += -Input.GetAxis("Vertical") * rotateSpeed * Time.deltaTime; rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX); rotationY += Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed; // added speed transform.rotation = Quaternion.Euler(rotationX, rotationY, 0); //make speed stay above 50 if (speed < 30) { speed = 30; } //check if player is rotating along y and adjust speed rotatingStart = transform.rotation.y; if (rotatingStart != rotatingUpdate) { speed -= 5; rotatingUpdate = rotatingStart; } else { if (speed < baseSpeed) { speed++; } } //move player CharacterController controller = GetComponent<CharacterController>(); transform.Translate(Vector3.forward * speed * Time.deltaTime); moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical" ), 0); moveDirection = transform.TransformDirection(moveDirection); controller.Move(moveDirection * speed * Time.deltaTime); } }
Can a similar method work to limit rotation if its caused by gravity/physics instead of player input?