- Home /
How to add sound to the movement of the radial slider
I want to make a sound when the temperature display radial slider in the above figure is rotated.
In the above UI, the light blue handle rotates.
The sound clicks every time it is rotated by 0.5 ° C, and I want to make a single sound continuous so that when I turn the handle, it clicks.
Below is a script with a radial slider attached to the light blue handle object in the figure.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class PointCircle4 : MonoBehaviour, IDragHandler
{
[Range(0f, 1f)]
public float value;
public float radius;
public float min;
public float max;
public TemperatureText temperatureText;
RectTransform rectTransfrom;
Vector2 parentPosition;
public void Start()
{
rectTransfrom = GetComponent<RectTransform>();
parentPosition = rectTransfrom.parent.transform.position;
}
public void OnDrag(PointerEventData data)
{
var diff = new Vector2(data.position.x - parentPosition.x, data.position.y - parentPosition.y);
float radian = Mathf.Atan2(diff.y, diff.x);
if (radian < -Mathf.PI / 2f)
{
radian += Mathf.PI * 2f;
}
radian = Mathf.Clamp(radian, min, max);
value = 1f - Mathf.InverseLerp(min, max, radian);
}
public void SetValue(float value)
{
this.value = value;
}
public void Update()
{
float radian = Mathf.Lerp(min, max, 1 - value);
//rectTransfrom.localPosition = new Vector2(Mathf.Cos(radian), Mathf.Sin(radian)) * radius;
Vector3 dir = new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
rectTransfrom.localPosition = dir * radius;
rectTransfrom.localRotation = Quaternion.LookRotation(Vector3.forward, dir);
temperatureText.value = value;
}
}
If you add an AudioSource to the object, how can you make it sound when the slider rotates?
The operation this time is moved by a variable resistor via Uduino.
I think the following part of the script is related to the event function.
public void SetValue(float value)
{
this.value = value;
}
222.jpg
(51.5 kB)
Comment