Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JeffreyReena · Apr 10, 2020 at 03:11 AM · tagcamera rotatetriggersfirst-person-controllermouse look

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges