_xiaofang/xiaofang/Assets/Script/Character/Skills/Skill_watering.cs

78 lines
2.3 KiB
C#
Raw Normal View History

2024-10-21 10:08:49 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Skill_watering : MonoBehaviour
{
private CharacterAin CharacterAin;
public float detectionRadius = 10f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ķ<EFBFBD>Χ
public LayerMask fireLayerMask; // <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>
// Start is called before the first frame update
void Start()
{
CharacterAin = transform.GetComponent<CharacterAin>();
}
/// <summary>
/// <20><>ʼ<EFBFBD><CABC>ˮ
/// </summary>
/// <param name="currentItem"><3E><>Ҫһ<D2AA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
public void StartWatering(GameObject currentItem)
{
CharacterAin.SetSkillAin(CharacterSkill.Watering);
// <20><><EFBFBD><EFBFBD><E2B8BD><EFBFBD>Ļ<EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
DetectAndLookAtFire();
if (currentItem.GetComponent<NoFire>() != null)
{
currentItem.GetComponent<NoFire>().StartFire();
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮ
/// </summary>
/// <param name="currentItem">һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
public void StopWatering(GameObject currentItem)
{
CharacterAin.SetSkillAin(CharacterSkill.NoSkill);
if (currentItem.GetComponent<NoFire>() != null)
{
currentItem.GetComponent<NoFire>().StopFire();
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD>Ļ<EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>ý<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>Դ
/// </summary>
private void DetectAndLookAtFire()
{
Collider[] objectsInRange = Physics.OverlapSphere(transform.position, detectionRadius, fireLayerMask);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>󣬲<EFBFBD><F3A3ACB2>ұ<EFBFBD>ǩΪ "fire" <20>Ķ<EFBFBD><C4B6><EFBFBD>
Collider nearestFire = null;
float minDistance = Mathf.Infinity;
foreach (Collider obj in objectsInRange)
{
if (obj.CompareTag("fire")) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>ǩ<EFBFBD><C7A9> "fire"
{
float distance = Vector3.Distance(transform.position, obj.transform.position);
if (distance < minDistance)
{
nearestFire = obj;
minDistance = distance;
}
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ý<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (nearestFire != null)
{
Vector3 directionToFire = (nearestFire.transform.position - transform.position).normalized;
directionToFire.y = 0; // <20><><EFBFBD>ֽ<EFBFBD>ɫ<EFBFBD><C9AB>ˮƽ<CBAE><C6BD><EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD><CFB3><EFBFBD><EFBFBD><EFBFBD>Դ
transform.forward = directionToFire;
}
}
}