• 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 Giantbean · Apr 04, 2013 at 11:00 PM · cameraaxisorbitpan

Trouble with Panning when camera is Orbited?

I have been all over the forum and Scripting reference trying to solve this issue. Sovacs1 wrote a great answer about camera movements a few years ago. He mentioned the problems that come from calculating the axis when the camera is orbited but I don't really understand his solution or how to implement what he explains in the post.

The trouble is I have a camera that is aimed at an object, I rotate around the object and zoom in and out. I also want to pan (or more correctly truck)the camera side to side and pedestal the camera up and down.

I can get the basic effect with:

     //Previous omitted code handles Zoom and Orbit based on the MouseOrbitZoom script 
             else if (Input.GetMouseButton(1))
             {
                 targetOffset.x += (transform.right * -Input.GetAxis("Mouse X") * panSpeed).x;
                 targetOffset.y += (Vector3.up * -Input.GetAxis("Mouse Y") * panSpeed).y;    
             }
    //Additional code clamps the Orbit so the model wont spin head over heels.

The rest of my code is a modification of a script from the wiki adding ease in and out of camera movements.

The trouble is that once you rotate the camera 90 degrees to look down at the object from above, your pedestal movement ignores the cameras rotation and acts on a world space rather then a local or perhaps Space.self causing the movement to look more like zooming in and out. Also the Trucking movement continues to move in the worlds x rather then recognizing the cameras new rotation.

I'm trying to make the camera Truck and Pedestal based on the cameras left and right and the cameras top and bottom rather then the worlds east and south and up and down as it were.

I would also like to clamp the side to side and up and down movement based on the model if possible or predefined limits similar to the way the wiki script clamps the rotation.

     //Clamp the vertical axis for the orbit
     yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);

Any Ideas of How I might go about doing this?

note: All the code is in C#

Comment
Add comment · Show 1
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 Josh707 · Apr 04, 2013 at 11:22 PM 0
Share

Is there an object that the camera is rotating around or parented to? You could try just lerping the position to one with a higher y value for it to move up and down at least.

1 Reply

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

Answer by whebert · Apr 05, 2013 at 01:53 AM

I believe something like the following might do what you want, that is if I understood you correctly; you wanted to be able to Truck and Pedestal relative to your camera's orientation, not in world space? And you also wanted to clamp the Truck/Pedestal's movement by some distance?

 using UnityEngine;
 using System.Collections;
 
 public class OrbitCamera : MonoBehaviour {
     
     public Transform target;
     public float maxOffsetDistance = 20f;
     public float orbitSpeed = 15f;
     public float panSpeed = .5f;
     public float zoomSpeed = 10f;
     
     private Vector3 targetOffset = Vector3.zero;
     
     // Use this for initialization
     void Start () {
     
         if(target != null) transform.LookAt(target);
     }
     
     // Update is called once per frame
     void Update () {
     
         if(target != null)
         {
             Vector3 targetPosition = target.position + targetOffset;
             
             // Left Mouse to Orbit
             if(Input.GetMouseButton(0))
             {
                 transform.RotateAround(targetPosition, Vector3.up, Input.GetAxis("Mouse X") * orbitSpeed);
                 float pitchAngle = Vector3.Angle(Vector3.up, transform.forward);
                 float pitchDelta = -Input.GetAxis("Mouse Y") * orbitSpeed;
                 float newAngle = Mathf.Clamp(pitchAngle + pitchDelta, 0f, 180f);
                 pitchDelta = newAngle - pitchAngle;
                 transform.RotateAround(targetPosition, transform.right, pitchDelta);
             }
             // Right Mouse To Truck, Pedestal
             if(Input.GetMouseButton(1))
             {
                 Vector3 offset = transform.right * -Input.GetAxis("Mouse X") * panSpeed + transform.up * -Input.GetAxis("Mouse Y") * panSpeed;
                 Vector3 newTargetOffset = Vector3.ClampMagnitude(targetOffset + offset, maxOffsetDistance);
                 
                 transform.position += newTargetOffset - targetOffset;
                 targetOffset = newTargetOffset;        
             }
             // Scroll to Zoom
             transform.position += transform.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
             
         }
     }
 }
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 Giantbean · Apr 05, 2013 at 03:38 PM 0
Share

Awesome! Thank you so much for your help.

avatar image Rispat-Momit · Jun 07, 2013 at 02:09 PM -1
Share

I had the same problem, thanks a lot for the help!!!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 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

Related Questions

How can I fix the touch orbit rotation? 0 Answers

Rotate according to the camera on Y and Z only! 2 Answers

How to make a camera move along and arbitrary set left-right axis 1 Answer

Orbit/Pan/Zoom target problem. 2 Answers

Rotate, pan and zoom camera lerp touch 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges