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 CristiBala24 · Jan 05, 2019 at 08:01 PM · rotationcamera rotatethird-personshooteryaxis

How to clamp Y rotation of camera?

Hi! So, i am trying to make a third person shooter and i have the movement/ camera script done, but i have one little problem. When i press right-click to aim, i cant clamp the y rotation of the camera and i can rotate the camera 360 degrees. Also the values are weird, when the camera goes up the x descends, and when the camera goes down the x value gets bigger.

Here is the script:

private const float Y_ANGLE_MIN = 0f; private const float Y_ANGLE_MAX = 50f;

 public Transform lookAt;
 public Transform cameraTransform;
 public Transform shootPoint;
 public float rotationSpeed;
 public bool isShooting;

 private Camera cam;

 private float distance = 5f;
 private float currX = 0f;
 private float currY = 0f;
 private float mouseX = 0f;
 private float mouseY = 0f;

 private void Start()
 {
     Cursor.lockState = CursorLockMode.Locked;

     cameraTransform = transform;
     cam = Camera.main;
 }

 private void Update()
 {
     currX += Input.GetAxis("Mouse X") * rotationSpeed;
     currY += Input.GetAxis("Mouse Y") * rotationSpeed * -1f;

     currY = Mathf.Clamp(currY, Y_ANGLE_MIN, Y_ANGLE_MAX);

     if(Input.GetKey(KeyCode.Mouse1))
     {
         if(isShooting == false)
         {
             cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
         }
         isShooting = true;
         cameraTransform.position = shootPoint.position;

         mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
         cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
         lookAt.Rotate(mouseX * Vector3.up);

         mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
         cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
     }
     if(Input.GetKeyUp(KeyCode.Mouse1))
     {
         isShooting = false;
     }
 }

 private void FixedUpdate()
 {
     if (isShooting == false)
     {
         Vector3 dir = new Vector3(0, 0, -distance);
         Quaternion rotation = Quaternion.Euler(currY, currX, 0);
         cameraTransform.position = lookAt.position + rotation * dir;
         cameraTransform.LookAt(lookAt.position);
     }
 }

I would be glad if someone could help me out :)!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JxWolfe · Jan 05, 2019 at 08:30 PM

  public Transform lookAt;
  public Transform cameraTransform;
  public Transform shootPoint;
  public float rotationSpeed;
  public float lockAngle = 45;
  public bool isShooting;
  private Camera cam;
  private float distance = 5f;
  private float currX = 0f;
  private float currY = 0f;
  private float mouseX = 0f;
  private float mouseY = 0f;
  private void Start()
  {
      Cursor.lockState = CursorLockMode.Locked;
      cameraTransform = transform;
      cam = Camera.main;
  }
  private void Update()
  {
      currX += Input.GetAxis("Mouse X") * rotationSpeed;
      currY += Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
      currY = Mathf.Clamp(currY, Y_ANGLE_MIN, Y_ANGLE_MAX);
      if(Input.GetKey(KeyCode.Mouse1))
      {
          if(isShooting == false)
          {
              cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
          }
          isShooting = true;
          cameraTransform.position = shootPoint.position;
          mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
          cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
          lookAt.Rotate(mouseX * Vector3.up);
          mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
          cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
      }
      if(Input.GetKeyUp(KeyCode.Mouse1))
      {
          isShooting = false;
      }
  }
  private void FixedUpdate()
  {
      if (isShooting == false)
      {
          Vector3 dir = new Vector3(0, 0, -distance);
          Quaternion rotation = Quaternion.Euler(currY, currX, 0);
          cameraTransform.position = lookAt.position + rotation * dir;
          cameraTransform.LookAt(lookAt.position);
          cameraTransform.eulerAngles = new Vector3(cameraTransform.eulerAngles.x,lockAngle,cameraTransform.eulerAngles.z);
      }
  }

that should the camera's y rotation...

Comment
Add comment · Show 2 · Share
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
avatar image CristiBala24 · Jan 05, 2019 at 09:03 PM 0
Share

I am not really sure what you meant by that. Could you please be more explicit?

avatar image JxWolfe · Jan 05, 2019 at 09:07 PM 0
Share

whoops... basically this:

 cameraTransform.eulerAngles = new Vector3(cameraTransform.eulerAngles.x,lockAngle,cameraTransform.eulerAngles.z);

should set the camera's y rotation to lockAngle no matter what you do. Sorry about that

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

134 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make the player rotate to where an object is being thrown? 1 Answer

How do I rotate player's camera to peak around objects. (first person shooter leaning) 1 Answer

I want the camera, which is following my spaceship, to rotate to wherever the cursor is looking 0 Answers

Unity Rotate Around and Orbit Question 0 Answers

Third-person Mouse Look Script? 0 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