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 Saucy_Noodle · Sep 12, 2013 at 05:46 PM · stay in place

c# scrolling zoom, lock X&Y in current position?

I am using a main camera to zoom into my gameObject using the scroll wheel. I've got the script working, but when I play, the main camera x and y values snap to 0 instead of staying where they are located in space. I need it a little off center for my GUI panel. How do I keep the camera position values at x = 0.28, y = -0.2 while I zoom?

 public class DirectionalButtons : MonoBehaviour 
 {
     public float distance;
     private float sensitivityDistance = -7.5f;
     private float damping = 2.5f;
     private float min = -4;
     private float max = -2;
     private Vector3 zdistance;
  
     void  Start ()
     {
         distance = -3f;
         distance = transform.localPosition.z;        
     }
     void  Update ()
     {
         distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
         distance = Mathf.Clamp(distance, min, max);
         zdistance.z = Mathf.Lerp(transform.localPosition.z, distance, Time.deltaTime * damping);
         transform.localPosition = zdistance;
     }
 }

   
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

2 Replies

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

Answer by ArkaneX · Sep 12, 2013 at 06:48 PM

The problem is, that you assign a zdistance vector, which by default have x and y set to 0. You have to use actual x and y from localPosition, by reading current local position, modifying z value, and then assigining it back:

 distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
 distance = Mathf.Clamp(distance, min, max);
 zdistance = transform.localPosition; // <- reading local position
 zdistance.z = Mathf.Lerp(transform.localPosition.z, distance, Time.deltaTime * damping);
 transform.localPosition = zdistance;
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
avatar image
0

Answer by Saucy_Noodle · Sep 12, 2013 at 08:15 PM

Here's how I fixed it:

 {
     public float distance;
     private float sensitivityDistance = -7.5f;
     private float damping = 2.5f;
     private float min = -4;
     private float max = -2;
     private Vector3 zdistance;
     
  
     void  Start ()
     {
         distance = -3f;
         distance = transform.localPosition.z;        
     }
     void  Update ()
     {
         distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
         distance = Mathf.Clamp(distance, min, max);
         zdistance.z = Mathf.Lerp(transform.localPosition.z, distance, Time.deltaTime * damping);
         transform.localPosition = zdistance;
         Vector3 pos = transform.position;
         pos.x = .2f;
         pos.y = -.15f;
         transform.position = pos;    
     }
 }
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 ArkaneX · Sep 12, 2013 at 09:38 PM 0
Share

I believe my answer was a valid one. You wanted to keep position of the camera, and that's exactly what code from my answer do. You didn't mention anything about requirement to be able to keep x and y in world position, only local position was present in script.

If you agree, please consider accepting my answer. If you disagree, please write what is wrong with it.

avatar image Saucy_Noodle · Sep 12, 2013 at 09:50 PM 0
Share

I had found this solution before you responded. Both work. Yours is easier.

avatar image ArkaneX · Sep 12, 2013 at 10:22 PM 0
Share

Thanks for accepting. As an additional info - your solution is going to work if world position of camera is equal to its local position. If camera would be a child of some other object, then things could get messy.

avatar image Saucy_Noodle · Sep 12, 2013 at 10:24 PM 0
Share

That makes sense. Thanks! I appreciate the help! There is so much to this software, it's easy to get lost.

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

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

Raycast Trouble 1 Answer

How can I increase the view distance of these trees leaves? (billboarding) 0 Answers

Demons Souls Fall death Camera 1 Answer

How can I scale the Joystick and TouchPad? 1 Answer

continuous time in collider box 2 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