2024-10-24 22:11:23 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public enum State
|
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
stand, // վ<><D5BE>״̬
|
|
|
|
|
move, // <20>ƶ<EFBFBD>״̬
|
|
|
|
|
dig // <20>ڿ<EFBFBD>״̬
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|
2024-10-25 19:14:25 +08:00
|
|
|
|
|
2024-10-24 22:11:23 +08:00
|
|
|
|
public class minercontorl : MonoBehaviour
|
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-28 12:11:56 +08:00
|
|
|
|
public Animator animator;
|
2024-10-24 22:11:23 +08:00
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20><>ǰ״̬
|
|
|
|
|
public State nowState = State.stand;
|
2024-10-24 22:11:23 +08:00
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// ״̬<D7B4><CCAC><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD> 0 <20><> 1 ֮<>䣩
|
2024-10-24 22:11:23 +08:00
|
|
|
|
[Range(0f, 1f)]
|
2024-10-29 11:35:16 +08:00
|
|
|
|
public float probabilityOfStand=0.2f; // վ<><D5BE>״̬<D7B4>ĸ<EFBFBD><C4B8><EFBFBD>
|
2024-10-24 22:11:23 +08:00
|
|
|
|
[Range(0f, 1f)]
|
2024-10-26 15:08:13 +08:00
|
|
|
|
public float probabilityOfMove=0.3f; // <20>ƶ<EFBFBD>״̬<D7B4>ĸ<EFBFBD><C4B8><EFBFBD>
|
2024-10-25 19:14:25 +08:00
|
|
|
|
[Range(0f, 1f)]
|
2024-10-29 11:35:16 +08:00
|
|
|
|
public float probabilityOfDig = 0.5f; // <20>ڿ<EFBFBD>״̬<D7B4>ĸ<EFBFBD><C4B8><EFBFBD>
|
2024-10-25 19:14:25 +08:00
|
|
|
|
|
|
|
|
|
// ״̬<D7B4>л<EFBFBD><D0BB><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
private float timer = 0;
|
|
|
|
|
public float ChangeStateTimer = 3f; // ״̬<D7B4>л<EFBFBD><D0BB><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public float moveSpeed = 2f; // <20>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
|
|
|
|
|
public Vector3 maxMovePos; // <20>ƶ<EFBFBD><C6B6><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
public Vector3 minMovePos; // <20>ƶ<EFBFBD><C6B6><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD>Сλ<D0A1><CEBB>
|
|
|
|
|
private Vector3 targetPosition; // <20><>ǰĿ<C7B0><C4BF>λ<EFBFBD><CEBB>
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-10-26 15:08:13 +08:00
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
public Vector3 digPosition; // <20>ڿ<EFBFBD><DABF><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
public float digTime = 5f; // <20>ڿ<EFBFBD><DABF>ij<EFBFBD><C4B3><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
private bool isDiging = false;
|
|
|
|
|
|
2024-10-28 15:33:52 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private Vector3 newV;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD>λ<EFBFBD><CEBB>
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>
|
2024-10-24 22:11:23 +08:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
2024-10-28 12:11:56 +08:00
|
|
|
|
//animator = transform.GetComponent<Animator>();
|
2024-10-25 19:14:25 +08:00
|
|
|
|
if (animator == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Animator component is missing.");
|
|
|
|
|
}
|
2024-10-24 22:11:23 +08:00
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ǰλ<C7B0><CEBB>
|
|
|
|
|
targetPosition = transform.position;
|
2024-10-28 15:33:52 +08:00
|
|
|
|
|
2024-10-29 11:35:16 +08:00
|
|
|
|
newV = new Vector3(digPosition.x, digPosition.y + Random.Range(-0.7f, 0.3f), digPosition.z);
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// ÿ֡<C3BF><D6A1><EFBFBD><EFBFBD>
|
2024-10-24 22:11:23 +08:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// ״̬<D7B4>л<EFBFBD><D0BB><EFBFBD>
|
|
|
|
|
if (nowState == State.stand) // ֻ<><D6BB><EFBFBD><EFBFBD>վ<EFBFBD><D5BE><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ɡ<EFBFBD><C9A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>״̬
|
2024-10-24 22:11:23 +08:00
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
if (timer >= ChangeStateTimer)
|
|
|
|
|
{
|
|
|
|
|
//nowState = RangNumber(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>״̬
|
|
|
|
|
SetState(RangNumber());
|
|
|
|
|
timer = 0;
|
|
|
|
|
}
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|
2024-10-25 19:14:25 +08:00
|
|
|
|
if (nowState==State.move&&!isDiging)
|
2024-10-24 22:11:23 +08:00
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
Move(targetPosition);
|
|
|
|
|
if (Vector3.Distance(transform.position, targetPosition)<0.01f)
|
|
|
|
|
{
|
|
|
|
|
SetState(State.stand);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (nowState==State.dig)
|
|
|
|
|
{
|
|
|
|
|
Autodig();
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20><><EFBFBD>õ<EFBFBD>ǰ״̬
|
2024-10-24 22:11:23 +08:00
|
|
|
|
void SetState(State state)
|
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
nowState = state;
|
2024-10-24 22:11:23 +08:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case State.dig:
|
2024-10-25 19:14:25 +08:00
|
|
|
|
animator.SetInteger("state", 2);
|
|
|
|
|
isDiging = true;
|
2024-10-24 22:11:23 +08:00
|
|
|
|
break;
|
2024-10-25 19:14:25 +08:00
|
|
|
|
case State.move:
|
|
|
|
|
animator.SetInteger("state", 1);
|
|
|
|
|
targetPosition = GetRandomPosition();
|
2024-10-24 22:11:23 +08:00
|
|
|
|
break;
|
|
|
|
|
case State.stand:
|
2024-10-25 19:14:25 +08:00
|
|
|
|
animator.SetInteger("state", 0);
|
2024-10-24 22:11:23 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
2024-10-24 22:11:23 +08:00
|
|
|
|
State RangNumber()
|
2024-10-25 19:14:25 +08:00
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܺͲ<DCBA><CDB2><EFBFBD><EFBFBD>й<EFBFBD>һ<EFBFBD><D2BB>
|
2024-10-24 22:11:23 +08:00
|
|
|
|
float totalProbability = probabilityOfStand + probabilityOfMove + probabilityOfDig;
|
|
|
|
|
if (totalProbability != 1f)
|
|
|
|
|
{
|
|
|
|
|
probabilityOfStand /= totalProbability;
|
|
|
|
|
probabilityOfMove /= totalProbability;
|
|
|
|
|
probabilityOfDig /= totalProbability;
|
|
|
|
|
}
|
2024-10-25 19:14:25 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ݸ<EFBFBD><DDB8>ʷ<EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
2024-10-24 22:11:23 +08:00
|
|
|
|
float randomValue = Random.value;
|
|
|
|
|
if (randomValue < probabilityOfStand)
|
|
|
|
|
{
|
|
|
|
|
return State.stand;
|
|
|
|
|
}
|
|
|
|
|
else if (randomValue < probabilityOfStand + probabilityOfMove)
|
|
|
|
|
{
|
|
|
|
|
return State.move;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return State.dig;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD>ָ<EFBFBD><D6B8>λ<EFBFBD><CEBB>
|
|
|
|
|
void Move(Vector3 pos)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD>ó<EFBFBD><C3B3><EFBFBD>
|
|
|
|
|
if (transform.position.x < pos.x)
|
2024-10-24 22:11:23 +08:00
|
|
|
|
{
|
|
|
|
|
transform.rotation = Quaternion.Euler(0, 0, 0); // <20><><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
transform.rotation = Quaternion.Euler(0, 180, 0); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// ƽ<><C6BD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
|
|
|
|
|
transform.position = Vector3.MoveTowards(transform.position, pos, moveSpeed * Time.deltaTime);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
public Vector3 GetRandomPosition()
|
|
|
|
|
{
|
|
|
|
|
float randomX = Random.Range(minMovePos.x, maxMovePos.x);
|
|
|
|
|
float randomY = Random.Range(minMovePos.y, maxMovePos.y);
|
|
|
|
|
float randomZ = Random.Range(minMovePos.z, maxMovePos.z);
|
|
|
|
|
|
|
|
|
|
return new Vector3(randomX, randomY, randomZ);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Autodig()
|
|
|
|
|
{
|
|
|
|
|
CheckDigPosition();
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڿ<EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
void CheckDigPosition()
|
2024-10-28 15:33:52 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Vector3.Distance(transform.position, newV) > 0.01f)
|
2024-10-24 22:11:23 +08:00
|
|
|
|
{
|
2024-10-25 19:14:25 +08:00
|
|
|
|
animator.SetInteger("state", 1);
|
2024-10-28 15:33:52 +08:00
|
|
|
|
Move(newV); // <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ڿ<EFBFBD>λ<EFBFBD><CEBB>
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|
2024-10-25 19:14:25 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StartDigging(); // <20><>ʼ<EFBFBD>ڿ<EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void StartDigging()
|
|
|
|
|
{
|
2024-10-26 17:24:48 +08:00
|
|
|
|
transform.rotation = Quaternion.Euler(0, 0, 0); // <20><><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD>
|
2024-10-25 19:14:25 +08:00
|
|
|
|
animator.SetInteger("state", 2);
|
|
|
|
|
StartCoroutine(DigRoutine()); // <20><><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD>Э<EFBFBD><D0AD>
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:14:25 +08:00
|
|
|
|
// <20>ڿ<EFBFBD>Э<EFBFBD><D0AD>
|
|
|
|
|
IEnumerator DigRoutine()
|
|
|
|
|
{
|
|
|
|
|
float workTimer = 0;
|
|
|
|
|
|
|
|
|
|
// <20>ڿ<EFBFBD><DABF><EFBFBD>ʱ
|
|
|
|
|
while (workTimer < digTime)
|
|
|
|
|
{
|
|
|
|
|
workTimer += Time.deltaTime;
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
isDiging = false;
|
|
|
|
|
SetState(State.stand);
|
|
|
|
|
}
|
2024-10-24 22:11:23 +08:00
|
|
|
|
}
|