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 cbstecher · May 16, 2017 at 04:18 PM · camerarotationtransformrotatearoundthird-person-camera

How to limit the angle of a camera with transform.RotateAround?

Okay, I've spent legitimately 3 days on this and I've become stumped by the 'transform.RotateAround' instruction. I'm trying to make a third person camera and it's all working fine except I can't figure out how to limit the Y Axis, so that it will stop if it gets too high or low around the character. Instead, what's happening now is if I move the camera too far it'll bug out at the 'poles' of the Y Axis.

 public class ThirdPersonCamera : MonoBehaviour
  {
      [SerializeField]
      private float distanceH;
      [SerializeField]
      private float distanceV;
      [SerializeField]
      private float smooth;
      [SerializeField]
      private Transform follow;
      [SerializeField]
      private float camSmoothDampTime = 0.1f;
      [Range (0f, 4f)][SerializeField]
      public float mouseSensitivityX = 1f;
      [Range(0f, 4f)][SerializeField]
      public float mouseSensitivityY = 1f;
  
      private Vector3 lookDir;
      private Vector3 restPosition;
      private Vector3 velocityCamSmooth = Vector3.zero;
  
      void Start()
      {
          follow = GameObject.FindWithTag("CameraTarget").transform;
      }
  
      void Update()
      {
          //Control with joystick
          transform.RotateAround(follow.transform.position, follow.up, mouseSensitivityX * Input.GetAxis("Joy X") * Time.deltaTime * 100);
          transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * Input.GetAxis("Joy Y") * Time.deltaTime * 100);
  
          //Control with mouse
          transform.RotateAround(follow.transform.position, follow.up, mouseSensitivityX * Input.GetAxis("Mouse X") * Time.deltaTime * 100);
          transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * Input.GetAxis("Mouse Y") * Time.deltaTime * 100);
      }
  
      void LateUpdate()
      {
          Vector3 characterOffset = follow.position;
  
          lookDir = characterOffset - this.transform.position;
          lookDir.Normalize();
  
          restPosition = characterOffset * distanceV - lookDir * distanceH;
          smoothPosition(this.transform.position, restPosition);
          transform.LookAt(follow);
      }
  
      void smoothPosition(Vector3 fromPos, Vector3 toPos)
      {
          this.transform.position = Vector3.SmoothDamp(fromPos, toPos, ref velocityCamSmooth, camSmoothDampTime);
      }
  }

This is what i have so far. Void Update is where the user controls are. I hope the internet can help. Thank you in advance!

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 toddisarockstar · May 17, 2017 at 11:03 PM

here is quick example of limiting an axis between two angles. Im just giving you am axaple for one axis. you whould need to write it again yourself for the X axis if thats what you want

 //adjust these next 2 floats to represent the angle where you stop.
     public float toplimit;
     public float bottomlimit;
 
 void Update(){
 
 
                 
                 float jy = Input.GetAxis ("Joy Y");
                  jy = jy + Input.GetAxis ("Mouse Y");
 
                 if (transform.eulerAngles.y > toplimit) {
                         if (jy > 0) {jy = 0;}}
                 if (transform.eulerAngles.y<bottomlimit) {
                 if(jy<0){jy=0;}}
 
                 transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * jy * Time.deltaTime * 100);
 }

 
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

An alternative to RotateAround for collisions 0 Answers

Where can I see rotation value from transform.RotateAround? 0 Answers

Keep camera at certain distance from character when rotating 3 Answers

Limit camera rotation with RotateAround 3 Answers

Camera rotation does not work properly 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