- Home /
I am working with first person controller, I cant control the clamp of main camera to specific rotation
Hi, I have a problem with my First Person Controller. I have attached player movement to FirstPerson and Mouselook script to camera. The project is a 3D Model house. Now my aim is whenever I'm near a wall and facing it, my camera rotation from left to right should be clamped between -90 and 90. other than that I should be able to rotate 360 degree. I really have no idea about how to clamp a camera rotation for left and right. i have attached an object and made it to trigger whenever it hits the object with tag Wall. i attached a separate script for trigger. but its not working. I really feel I have no control over my camera rotation.
pls find my scripts using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerMovenent : MonoBehaviour
{
public CharacterController controller;
public float speed = 10f;
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal") * speed;
float z = Input.GetAxis("Vertical") * speed;
Vector3 forwardMove = transform.forward * z;
Vector3 rightMove = transform.right * x;
controller.SimpleMove(forwardMove + rightMove);
}
My Mouselook script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
[SerializeField] private Transform playerBody;
float xRotation = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Cursor.lockState = CursorLockMode.Confined;
if(Input.GetKeyDown("escape"))
{
Cursor.lockState = CursorLockMode.None;
}
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation += mouseY;
if(xRotation > 90.0f)
{
xRotation = 90.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(270.0f);
}
else if(xRotation < -90.0f)
{
xRotation = -90.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(90.0f);
}
transform.Rotate(Vector3.left * mouseY);
playerBody.Rotate(Vector3.up * mouseX);
}
private void ClampXAxisRotationToValue(float value)
{
Vector3 eulerRotation = transform.eulerAngles;
eulerRotation.x = value;
transform.eulerAngles = eulerRotation;
}
}
My new trigger script, I have even tried attaching this to main camera but still not working
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wallcontroler : MonoBehaviour
{
private float yaw = 0f;
private float pitch = 0f;
private float minAngle = -90f;
private float maxAngle = 90f;
//Rotation Value
void Update()
{
//OnTriggerEnter(Collider collision);
}
void OnTriggerEnter(Collider collision)
{
if(collision.gameObject.tag == "Wall")
{
pitch = Mathf.Clamp(pitch, minAngle, maxAngle);
yaw = Mathf.Clamp(yaw, minAngle, maxAngle);
transform.eulerAngles = new Vector3(pitch, yaw, 0f);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612231001im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I want the camera, which is following my spaceship, to rotate to wherever the cursor is looking 0 Answers
colliding with child trigger? 0 Answers
How do you find the tag of the object that is a trigger? 1 Answer
Camera won't rotate while holding WASD keys 0 Answers
triggers and going through objects 2 Answers