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 Socapex · Feb 03, 2012 at 11:42 PM · cameraquaternionspaceshipsmoothfollowmouseorbit

Mouse Orbit to SmoothFollow and back (Euler to Quaternion possible?)

Hello everybody, I have been working on a space ship camera system for the last week and my head is just about to explode. I just can't do it and I've looked everywhere to no avail.

OK, I've modified the MouseOrbit script, so it orbits only when the user presses the right mouse button. It saves the last position so when the user clicks again, it doesn't reset the rotation. Now, when the user presses the "left/right" and "NoseThrust" keys, I'd like it if the camera would "SmoothFollow" the ship. Once the user stops pressing those keys, the camera should stay in that position. So far, I've kinda got that somewhat working (not really though).

The huge problem is when the user right-clicks on the screen (to orbit the camera again), there doesn't seem to be a way to convert my new rotated camera (using eulerAngles) to the weird, cryptic, Quaternion based MouseOrbit that the guys at Unity created. I just don't get it. I've tried many of the camera.ToSomething, or setting localRotation, rotation, rotation.eulerAngles as my last_x and last_y (see script below).

I've tried changing the Lerps in SmoothFollow to Quaternion.Lerp and Vector3.Lerp (rotation & position). I'm now experimenting with Quaternion.LookRotation which seems positive, but I get really bad jerking of my ship because the position is nuts (wrong), plus I can't get the Quaternions to follow the back of the ship... Help please!?

Here's the MouseOrbitClicked script, it actually works pretty well so hopefully it'll help somebody one day. The ClampAngle function is available in the Unity MouseOrbit script.

function LateUpdate () { if (target) { // Scrollwheel to set camera distance distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*2, distanceMin, distanceMax); //Follow player even when not pressing 3rd mouse button transform.position = last_rotation * Vector3(0.0, 0.0, -distance) + target.position; transform.rotation = last_rotation;

 if(Input.GetButtonDown("Fire2")){
   // Last position = the mouse start point
   x = last_x;
   y = last_y;
 }

 if(Input.GetAxis("Fire2")){
   x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
   y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
   y = ClampAngle(y, yMinLimit, yMaxLimit);
   var rotation = Quaternion.Euler(y, x, 0);
   var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
   transform.position = position;
   transform.rotation = rotation;

   last_rotation = rotation;
   last_x = x;
   last_y = y;
 }

} }

Edit:

OK, so I've got the smooth follow working using Quaternions (Slerp). So now I'm not using any Euler Angles in my script. Still, the rotation values from the Slerp don't match up with the ones from my mouse (last_x last_y). Any ideas why ?

Thanks in advance for any tips or help, or just mental support I'm going nuts with this issue :)

Comment
Add comment · Show 3
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 Socapex · Feb 03, 2012 at 11:49 PM 0
Share

Sorry for the bad code formatting... What happened to the good old [code] tags? QATO happened I guess :(

avatar image aldonaletto · Feb 05, 2012 at 05:26 PM 1
Share

@Socapex_2$$anonymous$$, to format code do the following: skip a blank line, place the tag <pre>, the code, then finish with a </pre> tag.

avatar image Socapex · Feb 08, 2012 at 04:03 AM 0
Share

Thanks a lot for the tip!

2 Replies

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

Answer by Socapex · Feb 08, 2012 at 04:16 AM

Finally got it. After studying the original Unity script, I kinda got a better idea of what they were doing with the Quaternions.

Here's the script, hopefully it helps someone someday. It will let you rotate the camera on right-click, from any point and zoom level it was left off. The ClampAngle function is a hack that works :)

var target : Transform; var zoom_distance = 100.0;

var zoom_distanceMin = 50.0; var zoom_distanceMax = 500.0; var camera_height = 10.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 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 Tuhljin · Jun 23, 2012 at 12:58 AM 0
Share

Thanks for posting your ClampAngle fix. It helped me solve a frustrating problem. I registered here just to give your answer an up vote... but apparently I can't do that on this account.

avatar image Socapex · Aug 31, 2012 at 12:49 AM 0
Share

Well that's very kind of you :) Glad it helped.

avatar image slideinsideways · Apr 02, 2013 at 10:53 PM 0
Share

Great Script!! I have been stuck on this for two days. Anyway, have you made any improvements? I am looking at using this for a RPG camera control. A classic problem is keeping the camera from going through walls etc. There are a few examples out there. Just wondering if you already have it built into a newer ver? If not, I will try and add it and shoot it your way.

avatar image Shepherd_L01 · Jun 10, 2014 at 02:43 PM 0
Share

Very Nice Script!

avatar image
0

Answer by Pady · Jun 02, 2013 at 09:10 PM

how can I modify the distance using the mouse orbit script, for example in a situation the distance is 10 and when in a smaller space the distance should turn 2, and 10 back after that?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Free" rotation about a sphere 0 Answers

Smooth camera script looking at left hand side of character. 0 Answers

Camera X and Y velocity HELP! 0 Answers

Quaternion - Euler Angles, weird variable swapping when converting 2 Answers

How do you check if Quaternion.Slerp has finished 5 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