Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 XxLinkxX · May 09, 2012 at 04:39 AM · cameramouseclickorbit

Camera Orbit on Left Click problem

I have this code, and it works for the most part. the only problem is that it only updates its position when i'm turning the character or turning the camera. if i make the character walk forward or backwards, the camera will maintain it's current position until i force it to rotate, then it will snap back to the character. Some help, please?

 var target : Transform;
 var zoom_distance = 5.0;
 var zoom_distanceMin = 5.0;
 var zoom_distanceMax = 20.0;
 var camera_height = 1.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -75;
 var yMaxLimit = 75;
 
 var positionDamping = 2.0;
 
 private var x = 0.0;
 private var y = 0.0;
 
 private var zoom = 0.0;
 
 private var last_rotation : Quaternion;
 
 private var doing_smoothfollow : boolean = false;
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
    
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target) {
         // Scrollwheel to set camera distance
 
          zoom_distance = Mathf.Clamp(zoom_distance - Input.GetAxis("Mouse ScrollWheel")*2, zoom_distanceMin, zoom_distanceMax);
 
         if(Input.GetButtonDown("Fire2")){
         // The Unity script inverses the x and y.
 
         // Also, start adding the mouse rotation to the current transform Euler Angles
         x = transform.eulerAngles.y;
         y = ClampAngle(transform.eulerAngles.x, yMinLimit, yMaxLimit);
         }
     
         if(Input.GetAxis("Fire2")){
 
         // Add mouse values to rotation, based on the previous camera rotation
 
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 
         // Clamp the y so weird things don't happen
 
          y = ClampAngle(y, yMinLimit, yMaxLimit);
 
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, camera_height, -zoom_distance) + target.position;
         transform.position = position;
         transform.rotation = rotation;
         last_rotation = rotation;
         }
     
     //Smooth follow if turning or tilting or the camera hasn't reached it's last rotation (before it was moved)
 
     else if(Input.GetAxis("Horizontal") || Input.GetAxis("NoseThrust") || doing_smoothfollow){
 
     //TODO: Implement doing_smoothfollow to let camera settle down behind ship
 
     wantedRotationAngle = Quaternion.Angle(transform.rotation, target.rotation);
     
     // Quaternion smooth follow
 
     transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, positionDamping * Time.deltaTime);
 
     transform.position = transform.rotation * Vector3(0.0, camera_height, -zoom_distance) + target.position;
 
     last_rotation = transform.rotation;
     }
 
     else {
         //Follow player even when not pressing 3rd mouse button
 
         transform.position = last_rotation * Vector3(0.0, camera_height, -zoom_distance) + target.position;
 
         transform.rotation = last_rotation;
     }
 
     }
 
 }
 static function ClampAngle (angle : float, min : float, max : float) {
 
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
 
         // Hacked to get it working with the Euler angles. Could be better I guess...
     if(angle > 360+min)
     
         //angle = 180 -angle;
         return angle;
     if(angle > 180)
         angle = -angle;
     return Mathf.Clamp (angle, min, max);
 
 }
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 AlucardJay · May 09, 2012 at 06:17 AM 0
Share

this looks like the 3D Buzz TPC tutorial , camera controller =]

I would suggest go back and watch all the theory video's in Section 2.

Here's my JS conversion of the camera script :

http://answers.unity3d.com/questions/246709/rotate-around-the-center-of-the-world-with-mouse.html

I'm not sure why your camera isn't snapping to follow the player when player moves. Please clarify :- does the camera snap to behind the character when the character is moving, or only when the character is moving and you move the mouse? By default, when the character is stationary the camera moves freely, when the character moves the camera snaps to behind the player.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Movement issue 1 Answer

Touch mouseorbit 5 Answers

Mouse Orbit with up/down based on target's up/down 0 Answers

A node in a childnode? 1 Answer

Mouse Orbit snapping issues 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