- Home /
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?
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.
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
Follow this Question
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