xi/Assets/scripts/Shoot.cs

144 lines
4.8 KiB
C#
Raw Normal View History

2024-11-27 00:53:01 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
2024-12-04 09:22:45 +08:00
using Unity.VisualScripting;
using TMPro;
2024-11-27 00:53:01 +08:00
public class Shoot : MonoBehaviour
{
public GameObject bulletPrefab; // <20>ӵ<EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
public GameObject firePrefab;
2024-12-04 09:22:45 +08:00
public GameObject missilePrefab;
2024-11-27 00:53:01 +08:00
public Transform[] bulletSpawn; // <20>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
2024-11-27 11:43:11 +08:00
public float fireRange = 10000f; // <20><><EFBFBD><EFBFBD>
2024-12-04 09:22:45 +08:00
public float missileSpeed = 1000f; // <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
public float distance;
public float attackInterval = 0.5f;
private float lastAttackTime = 0f;
2024-11-27 00:53:01 +08:00
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
2024-12-04 09:22:45 +08:00
//AutoAttack();
// <20><><EFBFBD><EFBFBD>
2024-11-27 00:53:01 +08:00
if (Input.GetMouseButtonDown(0))
{
2024-11-27 11:43:11 +08:00
if (Physics.Raycast(ray, out hitInfo, fireRange))
2024-11-27 00:53:01 +08:00
{
2024-11-27 11:43:11 +08:00
Vector3 targetPosition = hitInfo.point;
FireWeapon(targetPosition);
2024-12-04 09:22:45 +08:00
GoldFirePoint();
}
}
if (Input.GetMouseButtonDown(1))
{
if((transform.GetComponent<playercontroller>().currentMP -= 10)>=0)
{
Firemissile();
}
else
{
transform.GetComponent<playercontroller>().currentMP +=10;
}
}
}
void AutoAttack()
{
// <20><>ȡ<EFBFBD><C8A1>ը<EFBFBD><D5A8>Χ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>
Collider[] colliders = Physics.OverlapBox(transform.position, new Vector3(200, 50f, 200));
2024-11-27 11:43:11 +08:00
2024-12-04 09:22:45 +08:00
GameObject closestTarget = null;
float minDistance = float.MaxValue;
foreach (Collider collider in colliders)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
float dis = Vector3.Distance(transform.position, collider.transform.position);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD>Ӧ<EFBFBD><D3A6>Ŀ<EFBFBD><C4BF>
if (dis < minDistance)
{
minDistance = dis;
closestTarget = collider.gameObject;
}
}
if (closestTarget != null)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><E3B9A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ҫ<EFBFBD><D2AA>
if (Time.time - lastAttackTime < attackInterval) return;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD>
if (closestTarget.tag == "enemy")
{
FireWeapon(closestTarget.transform.position);
2024-11-27 11:43:11 +08:00
GoldFirePoint();
2024-12-04 09:22:45 +08:00
lastAttackTime = Time.time;
}
if (closestTarget.tag == "ore")
2024-11-27 00:53:01 +08:00
{
2024-12-04 09:22:45 +08:00
FireWeapon(closestTarget.transform.position);
GoldFirePoint();
lastAttackTime = Time.time;
2024-11-27 00:53:01 +08:00
}
}
}
2024-12-04 09:22:45 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
2024-11-27 00:53:01 +08:00
private void GoldFirePoint()
{
GameObject fireobj0 = Instantiate(firePrefab, bulletSpawn[0].position, bulletSpawn[0].rotation);
GameObject fireobj1 = Instantiate(firePrefab, bulletSpawn[1].position, bulletSpawn[1].rotation);
Destroy(fireobj0, 0.5f);
Destroy(fireobj1,0.5f);
}
2024-12-04 09:22:45 +08:00
//<2F><><EFBFBD><EFBFBD>
void Firemissile()
2024-11-27 00:53:01 +08:00
{
2024-12-04 09:22:45 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GameObject missile = Instantiate(missilePrefab, bulletSpawn[2].position, bulletSpawn[2].rotation);
2024-11-27 00:53:01 +08:00
2024-12-04 09:22:45 +08:00
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ŷ<EFBFBD><C5B7><EFBFBD>λ<EFBFBD>õ<EFBFBD> x <20><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>һ<EFBFBD><D2BB><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
float xDistance =200f; // <20><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EBA3AC><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
Vector3 targetPosition = missile.transform.position + missile.transform.forward * xDistance; // <20><> x <20><EFBFBD><E1B7A2>
2024-11-27 00:53:01 +08:00
2024-12-04 09:22:45 +08:00
// ʹ<><CAB9> DOMove ƽ<><C6BD><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
missile.transform.DOMove(targetPosition, 2f); // 2f<32><66><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ʱ<EFBFBD><EFBFBD><E4A3AC><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
2024-11-27 00:53:01 +08:00
}
2024-12-04 09:22:45 +08:00
//<2F><><EFBFBD><EFBFBD>
2024-11-27 11:43:11 +08:00
void FireWeapon(Vector3 targetPosition)
2024-11-27 00:53:01 +08:00
{
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn[0].position, bulletSpawn[0].rotation);
GameObject bullet1 = Instantiate(bulletPrefab, bulletSpawn[1].position, bulletSpawn[1].rotation);
2024-11-27 11:43:11 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD>õķ<C3B5><C4B7><EFBFBD>
Vector3 direction = targetPosition - this.transform.position;
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ת
Quaternion rotation = Quaternion.LookRotation(direction);
bullet.transform.rotation = rotation;
bullet1.transform.rotation = rotation;
2024-11-27 00:53:01 +08:00
2024-12-04 09:22:45 +08:00
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float distance = Vector3.Distance(bulletSpawn[0].position, targetPosition);
float distance1 = Vector3.Distance(bulletSpawn[1].position, targetPosition);
2024-11-27 00:53:01 +08:00
2024-12-04 09:22:45 +08:00
// <20><><EFBFBD>ݾ<EFBFBD><DDBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4A3A8><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float moveDuration = Mathf.Clamp(distance / 20f, 0.2f, 0.7f); // <20><EFBFBD><E8B6A8>С0.5<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD>
float moveDuration1 = Mathf.Clamp(distance1 / 20f, 0.2f, 0.7f); // ͬ<><CDAC>
float extensionDistance = 500f; // <20>ӳ<EFBFBD><D3B3>ľ<EFBFBD><C4BE><EFBFBD><EBA3AC><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
Vector3 extendedTargetPosition = targetPosition + direction.normalized * extensionDistance;
bullet.transform.DOMove(extendedTargetPosition, moveDuration);
bullet1.transform.DOMove(extendedTargetPosition, moveDuration1);
}
2024-11-27 00:53:01 +08:00
}