Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kyon · Jul 28, 2011 at 10:58 PM · cameramovehorizontal-slider

Horizontal slider and camera

Hello I have a problem with HorizontalSlider and Camera. I need to move the camera with the horizontalSlider. (for zoomIn and ZoomOut)

This is my current code:

public class TopPanelMenu : MonoBehaviour {

 public GUIStyle myStyle;
 public float hSliderValue = 0.0F;

 public Rect sliderRect;


 void OnGUI()
 {

     sliderRect = new Rect(200, 25, 170, 10);
     GUI.BeginGroup(new Rect(10.5f, 10.5f, 700, 200));
     {
         GameObject obj = GameObject.FindGameObjectWithTag("MainCamera");
         Camera cam = obj.GetComponent<Camera>() as Camera;

         GUI.Box(new Rect(0, 0, 550, 100), "Camera", GUI.skin.GetStyle("box"));
          // First button
         if (GUI.Button(new Rect(10, 25, 170, 40), "zoom", GUI.skin.GetStyle("button")))
         {
             
             
         }

         hSliderValue = GUI.HorizontalSlider(sliderRect, hSliderValue, 0.0f, 10.0f);

         cam.transform.Translate(0, 0, hSliderValue);

        
         
         
     }
     GUI.EndGroup();
 }

}

With this code, I can move the camera, but when I move it, this does not stop.

appreciate your help

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by YikYikHeiHei · Jul 28, 2011 at 11:07 PM

In the cam.transform.Translate(0, 0, hSliderValue);

You should not to use Translate, you should use position.

 cam.transform.position = Vector3(0,0,hSliderValue);


but if you want to zoom in and zoom out, I have a good idea to make it good.

change

 cam.transform.Translate(0, 0, hSliderValue); 

to

 cam.fieldOfView = hSliderValue;
Comment
Add comment · Show 1 · 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 Bunny83 · Jul 28, 2011 at 11:22 PM 0
Share

You are right that normally you zoom by changing the FOV, but not linearly. The slider will behave a bit strange that way. The zoo$$anonymous$$g would be much faster / stronger when it comes to small values. Normally you scale the fov by a scale factor.

avatar image
0

Answer by Bunny83 · Jul 28, 2011 at 11:18 PM

Translate will result in a relative movement. As long as your hSliderValue is not 0.0 it will move. The actual value will just determine how fast it is moving. Since you only accept values between 0 and 10 you can only move "forward".

I guess you don't intended an accelerated movement ;) so translate is not the right function for your problem then.

 Vector3 camPosition = cam.transform.position;
 camPosition.z = hSliderValue;
 cam.transform.position = camPosition;

If your camera should move from it's start position 0-10 forward you should save the start z value and add it: camPosition.z = hSliderValue + startValue;

If you wanted an accelerated movement it gets a bit tricky. You should define a deadzone around 0 and the slider value should go from -10 to 10. You should also multiply the movement by Time.deltaTime to make the movement framerate-independent.

ohh and btw. this code:

 GameObject obj = GameObject.FindGameObjectWithTag("MainCamera");
 Camera cam = obj.GetComponent<Camera>() as Camera;

is exactly the same as

 Camera cam = Camera.main;
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 Kyon · Jul 29, 2011 at 12:08 AM

Thank you very much by your help

I understand the Bunny83 idea, I did not think adding the current position in the z axis of the camera.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate cam with object, smooth boost and brake 3 Answers

Moving the camera "on rails" 2 Answers

How to make a camera that moves with the player? 1 Answer

How to make camera position relative to a specific target. 1 Answer

Player move and camera problem 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