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 /
  • Help Room /
avatar image
0
Question by YumeYume · Aug 26, 2015 at 01:42 AM · rotationrotatearoundorbitclamped rotation

Clamp camera orbit rotation using RotateAround

I am actually working on a game where the player will be able to control an airship with keyboard and camera, but not like in FPS where we control a human, where a big mouse drag can lead to an immediate 360° movement. I want the player to follow slowly the camera direction, depending of the vehicle. The best example I can give for this type of movement is vehicle movements in Borderlands 2.

 //Code was here, see updated version below

EDIT : I managed to clamp the height rotation using this post, adapting it with my needs. It now do what I want for height rotation, but it is messing with the side rotation.. For unknown reason, I can't go past 90° left or right. I didn't modify the transform.RotateAround for the side rotation, so it's something else in this that is causing the problem, but I can't figure it out.

 public Transform player;
     float heightRotation;
     float sideRotation;
     public float sensitivity;
     Vector3 initialVector;
 
     void Start () {
         sensitivity = 3;
         initialVector = transform.position - player.position;
         initialVector.x = 0;
     }
     
     // Update is called once per frame
     void Update () {
 
         //getting mouse inputs
         sideRotation = Input.GetAxis ("Mouse X");
         heightRotation = Input.GetAxis ("Mouse Y");
 
 
         //applying method seen on the post linked above, though I don't understand what ".x > 0 ? 1 : -1" is doing..
         Vector3 currentVector = transform.position - player.position;
         currentVector.x = 0;
         float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1);
         float upAngle = Mathf.Clamp(angleBetween + (heightRotation * sensitivity * Time.deltaTime), -75, 75);
         upAngle = upAngle - angleBetween;
         float sideAngle = sideRotation * sensitivity * Time.deltaTime;
 
         //applying calculated angles to the camera
         transform.RotateAround(player.position, Vector3.right, upAngle);
         transform.RotateAround(player.position, Vector3.up, sideAngle);
         transform.LookAt(player);
     }

Any clues about what is causing the side rotation unwanted clamping ? Also, it isn't clamping roughly like it does for the height rotation. As the height angle values are capped, if I try to go above it, nothing happens, the camera isn't moving at all. For the side rotation, it's like two forces opposing together, if I move my mouse rapidly, it will go above 90°, but go back when I stop moving my mouse. And last thing, no matter the angle I chose for the clamp, the side rotation will stop at 90°, tried different values from 15 to 120°, working perfectly for height rotation.. Thanks 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LessThanEpic · Aug 26, 2015 at 02:55 PM

This might have a couple of bugs in it (the camera might "pop" in a couple of locations), but I think it should get you close.

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour
 {
     public Transform player;
     public float sensitivityX = 5;
     public float sensitivityY = 5;
 
     void Start()
     {
         transform.LookAt(player.position);
     }
 
     void Update()
     {
         var sideRotation = Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
         var heightRotation = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
 
         var distance = (transform.position - player.position).magnitude;
 
         transform.position = player.position;
         var oldHeightRotation = transform.rotation.eulerAngles.x;
 
         transform.Rotate(-oldHeightRotation, 0, 0);
         transform.Rotate(0, sideRotation, 0);
 
         var max = 50f;
         var min = 320f;
       
         if (heightRotation > 0 && oldHeightRotation <= max + 1)
         {
             heightRotation = Mathf.Min(oldHeightRotation + heightRotation, max);
         }
         else if (heightRotation < 0 && oldHeightRotation >= min - 1) 
         {
             heightRotation = Mathf.Max(oldHeightRotation + heightRotation, min);
         }
         else
         {
             heightRotation = oldHeightRotation + heightRotation;
         }
 
         transform.Rotate(heightRotation, 0, 0);
 
         transform.Translate(transform.InverseTransformVector(transform.forward) * -distance, Space.Self);
     }
 }
 

 
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 YumeYume · Aug 27, 2015 at 01:38 AM 0
Share

Wow. I was sceptical with this at first, as I was absolutely trying to use the RotateAround function, which appears to be the core of the problem, as it's interacting with multiple variables at once, so it's hard to cap one of these values without messing with others.

Also, not using $$anonymous$$athf.Clamp solved another problem, as angles aren't going -1 but 359°, which was the problem in one of my tries. Using $$anonymous$$in and $$anonymous$$ax while checking the direction of the mouse was brilliant here, sir.

About the camera shaking in certain angles, it's because of the transform.LookAt(player) needs to be done after every move to keep the camera in the perfect angle at every frame. $$anonymous$$oved the function from Start to the end of the Update function, and all is perfectly smooth and doing what I want ! Thanks a lot sir, you just ended hours of pain, I can now keep moving (until the next problem, in a few $$anonymous$$utes probably) !

avatar image steeltomato · Oct 15, 2017 at 05:21 PM 0
Share

I couldn't figure out why but this approach resulted in the camera gradually "falling" towards the ground plane.

avatar image
0

Answer by steeltomato · Oct 15, 2017 at 05:28 PM

Here's an alternative approach that is fairly simple and works as long as you have sensible bounds. I'm using it for an free-look RTS-style camera. This isn't suitable for a free-space camera but maybe it will help others.

 // worldPosition is the location of the object you're rotating around
 private void UpdateRotationAndTilt(bool tiltButtonStatus, Vector3 worldPosition)
     {
         // Rotate and tilt
         if (!isTilting && tiltButtonStatus)
         {
             isTilting = true;
         }
         else if (isTilting && !tiltButtonStatus)
         {
             isTilting = false;
         }
 
         if (isTilting)
         {
             float currentTiltAngle = playerCamera.transform.eulerAngles.x;
             float sideRotation = Input.GetAxis("Mouse X") * rotateSensitivity * Time.deltaTime;
             float tiltRotation = Input.GetAxis("Mouse Y") * tiltSensitivity * Time.deltaTime;
 
             // When the mouse moves on the Y axis, tilt the camera in an arc around the point on the ground
             if ((tiltRotation > 0 && currentTiltAngle < tiltMaxAngle) || 
                 (tiltRotation < 0 && currentTiltAngle > tiltMinAngle))
             {
                 float angle = Mathf.Clamp(tiltRotation, tiltMinAngle - currentTiltAngle, tiltMaxAngle - currentTiltAngle);
 
                 playerCamera.transform.RotateAround(worldPosition, playerCamera.transform.right, angle);
             }
 
             // When the mouse moves on the X axis, rotate the camera in an arc around the point on the ground
             if (sideRotation != 0)
             {
                 playerCamera.transform.RotateAround(worldPosition, Vector3.up, sideRotation);
             }
         }
     }
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

28 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

Related Questions

Make GameObject rotate around another around random axis but with fixed distance 0 Answers

Why does RotateTowards rotate in an "orbit" rather than on the spot? Help me understand! 0 Answers

Manual RotateAround 0 Answers

constant rotation using keypress 0 Answers

Gameobject wont move after rotating it 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