I’m having some trouble.
I got to a point where I can make the scrollbar and the current camera scrolling system (which uses the mouse wheel to change the FOV) communicate.
But changing the value of the scrollbar seems to be a lot harder than I thought.
I can see this being done in two ways:
Either
The scrollbar value is controlled with the mouse wheel (and changes the FOV), until it reaches one of its limits (0 or 1), and then locks the FOV from changing any further (which would mean I’d be unable to fine-tune the FOV).
Or
The FOV is controlled with the mouse wheel (like it currently is) and changes the scrollbar value, until the value reaches its limits, and locks the FOV from going further (which would be the better option).
Either way, I still can’t figure out how to change the scrollbar’s value in any way.
Here’s the code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraScroll : MonoBehaviour {
Scrollbar bar;
public float minFov = 15f;
public float maxFov = 50f;
public float sensitivity = 12.5f;
void Start () {
bar = gameObject.GetComponent<Scrollbar> ();
}
void Update () {
float fov = Camera.main.fieldOfView;
if (bar.value >= 0 && bar.value <= 1) {
fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
}
}
I put it as a component of the UI Scrollbar, so that I would have an easier time referencing and calling the “Scrollbar (Script)” component inside the Scrollbar GameObject. (The FOV changing still works this way)
The “if” statement in the Update method is just a test, to see if they are communicating (the UI and camera). Basically I can set it to check between 0 and 0.5, or something like that, and then if I set the scrollbar value to above 0.5 before running the game, I won’t be able to change the FOV, which is the expected result.
Now I just need to implement the Scrollbar’s value change inside that if statement, which will essentially become the locking mechanism for the FOV.
So as you can see I got most of the work done, I just need some help finding the last piece.