67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
[System.Serializable]
|
||
|
|
public class Buterfly
|
||
|
|
{
|
||
|
|
public Transform objectRef;
|
||
|
|
public Vector3 targetPos;
|
||
|
|
Vector3 randomPos;
|
||
|
|
|
||
|
|
public Buterfly(Transform objectRef, Vector3 targetPos)
|
||
|
|
{
|
||
|
|
this.objectRef = objectRef;
|
||
|
|
this.targetPos = targetPos;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Ai()
|
||
|
|
{
|
||
|
|
if(Vector3.Distance(objectRef.position, targetPos) < 0.1f)
|
||
|
|
{
|
||
|
|
targetPos = GetRandomPos(objectRef.parent.position);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
//objectRef.position = Vector3.Lerp(objectRef.position, targetPos, Time.deltaTime * 2);
|
||
|
|
objectRef.Translate(Vector3.forward * Time.deltaTime * 2f);
|
||
|
|
objectRef.rotation = Quaternion.Lerp(objectRef.rotation, Quaternion.LookRotation(targetPos - objectRef.position, objectRef.forward), Time.deltaTime * 2);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Vector3 GetRandomPos(Vector3 origionPos)
|
||
|
|
{
|
||
|
|
return origionPos + new Vector3(Random.Range(-2, 2f), Random.Range(0, 2f), Random.Range(-2, 2f));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class ButerflyMain : MonoBehaviour
|
||
|
|
{
|
||
|
|
public int count;
|
||
|
|
public GameObject buterflyRes;
|
||
|
|
public Buterfly[] buterflies;
|
||
|
|
|
||
|
|
// Start is called before the first frame update
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
buterflies = new Buterfly[count];
|
||
|
|
for(int i = 0; i < count; ++i)
|
||
|
|
{
|
||
|
|
GameObject clone = Instantiate(buterflyRes, Buterfly.GetRandomPos(transform.position), Quaternion.Euler(Vector3.right * Random.Range(0, 360f)), transform);
|
||
|
|
buterflies[i] = new Buterfly(clone.transform, Buterfly.GetRandomPos(transform.position));
|
||
|
|
buterflies[i].objectRef.GetComponent<MeshRenderer>().material.SetFloat("_Index", Random.Range(0, 9));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
if (Vector3.Distance(transform.position, Camera.main.transform.position) > 30) { return; }
|
||
|
|
|
||
|
|
for (int i = 0; i < count; ++i)
|
||
|
|
{
|
||
|
|
buterflies[i].Ai();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|