• 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 Falantar · Jun 13, 2012 at 02:24 PM · camerajavascriptrotateorbit

Slowly orbit camera on keypress

I used the generic orbit camera script and tweaked it to only rotate 90 degrees when either the left or right arrow key are pressed. It works well, the only thing now is I'd like to get it to kind of swing around as opposed to instantly snapping to the position.

I've read most of the other questions regarding orbiting objects but I'm pretty new to unity and don't really know how to apply them to my situation. Here's the code:

  var target : Transform;
  var targetOffset = Vector3.zero;
  var distance = 4.0;
 
 var lineOfSightMask : LayerMask = 0;
 var closerRadius : float = 0.2;
 var closerSnapLag : float = 0.2;
 
 var xSpeed = 200.0;
 var ySpeed = 80.0;
 
 var yMinLimit = 45;
 var yMaxLimit = 45;
 
 private var currentDistance = 10.0;
 private var x = 0.0;
 private var y = 0.0;
 private var distanceVelocity = 0.0;
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
     currentDistance = distance;
     
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target) {
         if(Input.GetKeyDown(KeyCode.LeftArrow)){
             x += 90;
          }
          if(Input.GetKeyDown(KeyCode.RightArrow)){
             x -= 90;
          }
          
          y = ClampAngle(y, yMinLimit, yMaxLimit);
                 
         var rotation = Quaternion.Euler(y, x, 0);
         var targetPos = target.position + targetOffset;
         var direction = rotation * -Vector3.forward;
         
         var targetDistance = AdjustLineOfSight(targetPos, direction);
         currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, distanceVelocity, closerSnapLag * .3);
         
         transform.rotation = rotation;
         transform.position = targetPos + direction * currentDistance;
     }
 }
 
 function AdjustLineOfSight (target : Vector3, direction : Vector3) : float
 {
     var hit : RaycastHit;
     if (Physics.Raycast (target, direction, hit, distance, lineOfSightMask.value))
         return hit.distance - closerRadius;
     else
         return distance;
 }
 
 static function ClampAngle (angle : float, min : float, max : float) {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return Mathf.Clamp (angle, min, max);
 }
 
 @script AddComponentMenu("Third Person Camera/Mouse Orbit"

)

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
Best Answer

Answer by Sokco816 · Jun 13, 2012 at 07:09 PM

Add this to your camera:

 var object : Transform;
 private var lastRotation : Vector3;
 var rotateSpeed : float;
 private var swing : float;
 var toRight : boolean = false;
 var toLeft : boolean = false;
 
 function Start(){
 lastRotation = transform.eulerAngles;
 }
 
 function Update(){
 //Always look at the object
 transform.LookAt(object);
 
 //rotate 90 degrees to right
 if(Input.GetKeyDown("right") && toLeft==false){
 toRight = true;}
 if(toRight && toLeft==false && Mathf.RoundToInt(transform.eulerAngles.y) != Mathf.RoundToInt(lastRotation.y)-90){
 swing = Mathf.Abs(Mathf.RoundToInt(lastRotation.y)-90-Mathf.RoundToInt(transform.eulerAngles.y));
 transform.RotateAround(object.position, -transform.up, rotateSpeed * swing *Time.deltaTime);}
 if(Mathf.RoundToInt(transform.eulerAngles.y) <= Mathf.RoundToInt(lastRotation.y)-90){
 toRight = false;
 lastRotation = transform.eulerAngles;}
 
 //rotate 90 degrees to left
 if(Input.GetKeyDown("left") && toRight==false){
 toLeft = true;}
 if(toLeft && toRight==false && transform.eulerAngles.y != lastRotation.y+90){
 swing = Mathf.Abs(Mathf.RoundToInt(lastRotation.y)+90-Mathf.RoundToInt(transform.eulerAngles.y));
 transform.RotateAround(object.position, transform.up, rotateSpeed * swing * Time.deltaTime);}
 if(Mathf.RoundToInt(transform.eulerAngles.y) >= Mathf.RoundToInt(lastRotation.y)+90){
 toLeft = false;
 lastRotation = transform.eulerAngles;}
 }

Good luck with the project!

Comment
Add comment · Show 4 · 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 Falantar · Jun 13, 2012 at 07:13 PM 0
Share

Works like a charm! $$anonymous$$uch appreciated. I'm going to try and read through it to make sure I understand what's going on.

avatar image Sokco816 · Jun 13, 2012 at 07:40 PM 0
Share

No problem, it's my pleasure!

avatar image Falantar · Jun 13, 2012 at 07:51 PM 0
Share

I noticed that the angle (y-axis) slightly di$$anonymous$$ishes each time the camera rotates. You can test it better by setting the speed up higher, like to about 10. I also noticed that the camera will rotate twice occasionally. Any idea on what might be the issue?

avatar image Sokco816 · Jun 14, 2012 at 01:17 AM 0
Share

The camera sometimes rotates twice because it is moving so quickly, that it actually misses it's goal rotation and has to move on to the next goal rotation. To get it more precise, you can try changing

  if(toLeft && toRight==false && transform.eulerAngles.y < lastRotation.y+90){

to

  if(toLeft && toRight==false && $$anonymous$$athf.RoundToInt(transform.eulerAngles.y) < $$anonymous$$athf.RoundToInt(lastRotation.y)+90){

and

  if(toRight && toLeft==false && $$anonymous$$athf.RoundToInt(transform.eulerAngles.y) > $$anonymous$$athf.RoundToInt(lastRotation.y)-90){

to

  if(toRight && toLeft==false && $$anonymous$$athf.RoundToInt(transform.eulerAngles.y) > $$anonymous$$athf.RoundToInt(lastRotation.y)-90){

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Set Max Rotation On Weapon Sway 0 Answers

Script not function if mouse is on object 1 Answer

Click on collider to rotate camera 1 Answer

Mouse Orbit Reset Camera 0 Answers

Help with camera movement using mobile input 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