• 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 SpitefulGOD · Nov 11, 2013 at 12:47 PM · cameralerporbit

Rotate camera outside of movement lerp?

I modified a standard mouse orbit script to allow for smooth movement between targets using lerp although I now have the issue in that the mouse drag functionality to rotate the object around the object is also using lerp so the object moves off centre and slowly moves back to centre which is the effect I'm trying to get rid of, as you can see below I've moved the X/Y movement outside of the lerp although it doesn't seem to be doing anything, can anyone point me in the correct direction?

 using UnityEngine;
 using System.Collections;
 
 public class OrbiterCamera : MonoBehaviour {
     // Min|Max distance of the camera from the target, set when the target is set but these are defaults.
     private float minDistance = 5.0f; 
     private float maxDistance = 20.0f;
 
     
     public float MinDistance = 5.0f; 
     public float MaxDistance = 20.0f;
     public float TransformSpeed = 3.0f;
     
     [SerializeField]
     private Transform target;
     public Transform Target {
         get {
             return target;
         }
         set {
             if(value != null && value != target) {
                 cameraSpeed = Mathf.Abs(Vector3.Distance(transform.position, value.position)) / TransformSpeed;
                 float size = Mathf.Max(Mathf.Max(value.renderer.bounds.size.x, value.renderer.bounds.size.y), value.renderer.bounds.size.z);
                 minDistance = size * MinDistance;
                 maxDistance = size * MaxDistance;
                 if(distance == 0f)
                     distance = size * 3;
             }
             target = value;
         }
     }
     private float distance = 0f;
     private float cameraSpeed = 1.0f;
     public float xSpeed = 175.0f;
     public float ySpeed = 75.0f;
     public float yMinLimit = -80.0f; //Lowest vertical angle in respect with the target.
     public float yMaxLimit = 80.0f;
     private float x = 0.0f;
     private float y = 0.0f;
     
     void Start () {
         this.Target = this.Target;
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
     void OnGUI() {
         GUI.Label(new Rect(10,10,100,20), x.ToString());
     }
     
     void Update () {
         //Detect mouse drag;
         if (target) {
             //Zooming with mouse
             distance += Input.GetAxis("Mouse ScrollWheel")*distance;
             distance = Mathf.Clamp(distance, minDistance, maxDistance);
         }
         if(Input.GetMouseButton(0))   {
             x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
             y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
         }
         y = ClampAngle(y, yMinLimit, yMaxLimit);
         
         //Quaternion rotation = Quaternion.Euler(0, 0, 0);
         Vector3 position = new Vector3(0.0f, 0.0f, -distance) + target.position;
         position = Vector3.Lerp (transform.position, position, cameraSpeed * Time.deltaTime);



         // Allow mouse movement outside of the Lerp. although this doesn't seem to do anything :(
         Quaternion rotation = Quaternion.Euler(y, x,0);
         position = rotation  * new Vector3(0,0,-distance) + position;
         transform.position = position;


         // Get the rotation from the new position
         Vector3 stareAt = (target.position - transform.position);
         transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation(stareAt), cameraSpeed * Time.deltaTime);
     }
     static float ClampAngle (float angle, float min, float max) {
         if (angle < -360)
             angle += 360;
     
         if (angle > 360)
             angle -= 360;
     
         return Mathf.Clamp(angle, min, max);
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

Rotate, pan and zoom camera lerp touch 0 Answers

Mouse Orbit snapping issues 0 Answers

Easy transition between multiple views? 1 Answer

Single Camera Lerping between three characters? (code provided) 0 Answers

How Do I Simulate First-Person Without Clipping Player Model? 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