• 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 sam-luk · Jan 20, 2012 at 12:48 PM · camerajavascriptresetorbitreset-position

Mouse Orbit Reset Camera

Hi

I was wondering, is there a Mouse Orbit script which allows the user to click a button and reset the camera?

I've tried just re-calling the start function but that doesn't seem to work

Here's an example of my code, I'VE ADDED IN THE "ResetCamera" but it doesn't seem to work

 var target : Transform; 
 
 var targetHeight = 2.0; 
 var distance = 25; 
 
 var maxDistance = 80; 
 var minDistance = 10; 
 
 var xSpeed = 250.0; 
 var ySpeed = 120.0; 
 
 var yMinLimit = -150; 
 var yMaxLimit = 100; 
 
 var zoomRate = 20; 
 
 private var x = 0.0; 
 private var y = 0.0; 
 
 private var initialPosition : Vector3;
 private var initialRotation : Quaternion;
 
 @script AddComponentMenu("Camera-Control/MouseOrbitZoom") 
 
 function Start () { 
     var angles = transform.eulerAngles; 
     x = angles.y; 
     y = angles.x; 
 
    initialPosition = transform.position;
    initialRotation = transform.rotation;
 
    // Make the rigid body not change rotation 
       if (rigidbody) 
       rigidbody.freezeRotation = true; 
 } 
 
 function ResetCamera () {
   transform.position = initialPosition;
   transform.rotation = initialRotation;
   print(transform.position.x);
 }
 
 function LateUpdate () { 
    if(!target) 
       return; 
     
    // If either mouse buttons are down, let them govern camera position 
    if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) 
    { 
    x += Input.GetAxis("Mouse X") * xSpeed * 0.02; 
    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02; 
    }
 
    distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance); 
    distance = Mathf.Clamp(distance, minDistance, maxDistance); 
     
    y = ClampAngle(y, yMinLimit, yMaxLimit); 
     
    var rotation:Quaternion = Quaternion.Euler(y, x, 0); 
    var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0)); 
     
    transform.rotation = rotation; 
    transform.position = position; 
    
    print(transform.position.x);
 } 
 
 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); 
 }

Thanks Sam

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 MountDoomTeam · Dec 20, 2012 at 07:02 PM 0
Share

same question I think perhaps because it's late update, it doesn't listen to OnGUI.

avatar image Giantbean · Apr 10, 2013 at 05:09 PM 0
Share

what about having it use a "GetkeyDown" using a key code ins$$anonymous$$d of an OnGUI. is that effected by the late update as well?

it seems the code needs to effect desiredRotation and desiredDistance or the reset will be overwritten the frame after the key is pressed.

I can get the camera to rotate back for one frame with

 private Quaternion intCamRot = Quaternion.Euler(0.0f,1.0f,0.0f)
 void Start()
 {
   intCamRot = transform.rotation;
 }
 
 void ResetCamera()
 {
    transform.rotation = intCamRot;
 }

but the position is not effected with

 private Vector3 intCamPos;
 
 void Start()
 {
   intCamPos = transform.position;
 }
 
 void ResetCamera()
 {
    transform.position = intCamPos;
 }

and the effect or the rotation only lasts one frame, likely do to the late update as ZoomDomain suggests.

I have looked at Pedagogic368's code but have not been able to convert the solution over to the $$anonymous$$ouseOrbitZoom Camera.

Anyone else have any ideas or a working reset button?

Do I need to reset/reload the entire scene?

a few related posts that have failed to solve this issue:

http://forum.unity3d.com/threads/3891-Reset-camera-to-original-start-point

http://answers.unity3d.com/questions/414919/create-reset-button-to-main-camera.html

http://answers.unity3d.com/questions/56389/reset-camera-position-mouselook.html

The community definitely needs and wants a fix to this issue.

avatar image Giantbean · Apr 16, 2013 at 02:55 PM 0
Share

Tested with update and late update. That's not the issue the position and rotation only reset for one frame or while the button is held down. It may be a static variable or a needed Awake() function. perhaps the static ClampAngle is overriding the ResetCamera. I'll keep testing.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Programming A Reset Scene 0 Answers

Slowly orbit camera on keypress 1 Answer

Orbit around player, but allow re-centering of camera via button press? 2 Answers

3drd person camera reset 0 Answers

Script not function if mouse is on object 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