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 Mansquatch · Dec 13, 2012 at 09:46 PM · camerarotationgui

Bind horizontal scrollbar to camera control

I am trying to make it so that the user has to be moving the scrollbar right and left in order to rotate the camera. This is the script that I have:

 using UnityEngine;
 using System.Collections;
  
 [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
 public class OrbitCamWithXOffset: MonoBehaviour
 {
     public Transform target;
     public float zoomMultiplier = 1.2f;
     public float distance = 0.0f;
     public float xSpeed = 45.0f;
     public float ySpeed = 45.0f;
     public float rotLeftMax = 135f;
     public float rotRightMax = 210f;
     public float distanceMin = 1.2f;
     public float distanceMax = 2.0f;
     public Rect camSliderRect;
     
     private float hSliderValue = 10f;
     private float x = 0.0f;
     private float y = 0.0f;
     private bool camRotLeft = false;
     private bool camRotRight = false;
     
  
     // Use this for initialization.
     void Start (){
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
  
         // Make the rigid body not change rotation.
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
     
     void OnGUI(){
         camSliderRect = new Rect(10,10,200,40);
         hSliderValue = GUI.HorizontalScrollbar(camSliderRect, x, 1, rotLeftMax, rotRightMax);
     }
  
     void LateUpdate (){
         if (target && Input.GetMouseButton(0)) {
             x += Input.GetAxis ("Mouse X") * xSpeed * distance * 0.02f;
             x = ClampAngle (x, rotLeftMax, rotRightMax);
  
             Quaternion rotation = Quaternion.Euler (y, x, 0);
             distance = Mathf.Clamp (distance - Input.GetAxis ("Mouse ScrollWheel") * zoomMultiplier, distanceMin, distanceMax);
             
             Vector3 negDistance = new Vector3 (0.0f, 0.0f, -distance);
             Vector3 position = rotation * negDistance + target.position;
  
             transform.rotation = rotation;
             transform.position = position;
         }
     }
  
     // Clamp angle to 360 degrees.
     public static float ClampAngle (float angle, float min, float max){
         if (angle < -360f)
             angle += 360f;
         if (angle > 360f)
             angle -= 360f;
         return Mathf.Clamp (angle, min, max);
     }
 }

This, currently, rotates the camera when you have the mouse button down. You see the scrollbar moving relative to the camera. I want it so that you have to have the mouse down on the scrollbar's thumb in order to rotate the camera. Any 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Dec 13, 2012 at 10:01 PM

You aren't using the scrollbar's value:

 hSliderValue = GUI.HorizontalScrollbar(camSliderRect, x, 1, rotLeftMax, rotRightMax);

Looks like you are trying to compute the value based on mouse position???

Anyway, try:

 hSliderValue = GUI.HorizontalScrollbar(camSliderRect, hSliderValue, 1, rotLeftMax, rotRightMax);

and use hSliderValue as THE slider value for your rotation.

Comment
Add comment · Show 2 · 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 clunk47 · Dec 13, 2012 at 10:03 PM 0
Share

I'd say accept Dave's answer.

avatar image Mansquatch · Dec 13, 2012 at 10:09 PM 0
Share

That doesn't solve my problem. Where do you use hSliderValue as THE slider value for the rotation. Also, LateUpdate needs to have a check for if you are using the slider before it executes the nested code. What is the check for if the user is currently using the slider thumb>

Also, I have the mouse bound to X as just a temporary implementation until I get this working.

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

11 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

Related Questions

Attach image to button 1 Answer

Problem - Getting Object to Face Camera Plane 2 Answers

3D effect with new UI system 1 Answer

Getting the camera to look at a direction? 1 Answer

Limiting object rotation using GUI.Buttons? 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