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 serdechny · Aug 10, 2019 at 08:46 AM · cameravertical

how do I limit camera vertical rotation

can you please help me with limiting camera's vertical rotation with my code? heres the code:

public float speed = 5, sensetivity;

private PlayerMoto motor;

public GameObject hands;

public camera cam;

// Start is called before the first frame update

void Start() { motor = GetComponent(); cam = GetComponent(); } // Update is called once per frame void Update() { //cam.transform.localEulerAngles = new Vector3(cam.transform.localEulerAngles.x, 0, 0); float _xmove = Input.GetAxis("Horizontal"); float _zmove = Input.GetAxis("Vertical");

  Vector3 _moveHorizontal = transform.right * _xmove;
  Vector3 _movVertical = transform.forward * _zmove;
  Vector3 _velocity = (_moveHorizontal + _movVertical) * speed;
  motor.Move(_velocity);
  float _yRot = Input.GetAxis("Mouse X");
  Vector3 _rotation = new Vector3(0f, _yRot, 0f) * sensetivity;
  motor.Rotate(_rotation);
  float _xRot = Input.GetAxis("Mouse Y");
  Vector3 _cameraRotation = new Vector3(_xRot, 0f, 0f) * sensetivity;
  motor.RotateCamera(_cameraRotation);
 
  if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) {
      for (; speed <= 15; speed++)
      {
          speed = 15;
      }
  }
  else
  {
      speed = 10;
  }

}

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 SpaghettiCloud · Aug 10, 2019 at 09:47 AM

You could implement a clamping mechanism similar to this one:

  public class PlayerLook : MonoBehaviour
 {
     [SerializeField] private string mouseXInputName, mouseYInputName; 
     [SerializeField] private float mouseSensitivity;
 
     [SerializeField] private Transform playerBody;
 
     private float xAxisClamp;
 
     private void Awake()
     {
         LockCursor();
         xAxisClamp = 0.0f;
     }
 
 
     private void LockCursor()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     private void Update()
     {
         CameraRotation();
     }
 
     private void CameraRotation()
     {
         float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
 
         xAxisClamp += mouseY;
 
         if(xAxisClamp > 90.0f)
         {
             xAxisClamp = 90.0f;
             mouseY = 0.0f;
             ClampXAxisRotationToValue(270.0f);
         }
         else if (xAxisClamp < -90.0f)
         {
             xAxisClamp = -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;
     }
 }

I found this code here, please visits its creator's github page [Acacia-Developer][1]
[1]: https://github.com/Acacia-Developer

Comment
Add comment · Show 1 · 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 serdechny · Aug 10, 2019 at 11:08 AM 0
Share

nothing working Im trying the best as I can but I still fail

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

171 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 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

Camera locks on y axis on start 1 Answer

How to make camera position relative to a specific target. 1 Answer

Raycast - Making ray shoot down vertically -Vector.up 1 Answer

Vertically Stationary Camera for 2D? 0 Answers

limit camera vertical rotation 1 Answer


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