挂在需要散开的物体上
using UnityEngine;
public class item : MonoBehaviour
{
private Vector3 initPosl;
void Start()
{
initPosl = transform.position;
}
public void ChangePos(float value,Vector3 rootPos)
{
Vector3 dir = initPosl - rootPos;
transform.position = dir * value+initPosl;
}
}
root为散开的中心点
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public List<item> allObjs;
public Slider slider;
public float interval = 1;
public Transform root;
private void Start()
{
slider.onValueChanged.AddListener(val => SliderValueChanged(val));
}
void SliderValueChanged(float value)
{
for (int i = 0; i < allObjs.Count; i++)
{
allObjs[i].ChangePos(value,root.position);
}
}
}