144 lines
4.8 KiB
C#
144 lines
4.8 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using DG.Tweening;
|
||
using Unity.VisualScripting;
|
||
using TMPro;
|
||
public class Shoot : MonoBehaviour
|
||
{
|
||
public GameObject bulletPrefab; // 子弹预制体
|
||
public GameObject firePrefab;
|
||
public GameObject missilePrefab;
|
||
public Transform[] bulletSpawn; // 子弹生成位置
|
||
public float fireRange = 10000f; // 射程
|
||
public float missileSpeed = 1000f; // 导弹速度
|
||
public float distance;
|
||
public float attackInterval = 0.5f;
|
||
private float lastAttackTime = 0f;
|
||
void Update()
|
||
{
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit hitInfo;
|
||
|
||
//AutoAttack();
|
||
// 射击
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
if (Physics.Raycast(ray, out hitInfo, fireRange))
|
||
{
|
||
Vector3 targetPosition = hitInfo.point;
|
||
FireWeapon(targetPosition);
|
||
GoldFirePoint();
|
||
}
|
||
}
|
||
if (Input.GetMouseButtonDown(1))
|
||
{
|
||
if((transform.GetComponent<playercontroller>().currentMP -= 10)>=0)
|
||
{
|
||
Firemissile();
|
||
}
|
||
else
|
||
{
|
||
transform.GetComponent<playercontroller>().currentMP +=10;
|
||
}
|
||
|
||
}
|
||
}
|
||
void AutoAttack()
|
||
{
|
||
// 获取爆炸范围内的所有目标
|
||
Collider[] colliders = Physics.OverlapBox(transform.position, new Vector3(200, 50f, 200));
|
||
|
||
GameObject closestTarget = null;
|
||
float minDistance = float.MaxValue;
|
||
|
||
foreach (Collider collider in colliders)
|
||
{
|
||
// 计算与目标的距离
|
||
float dis = Vector3.Distance(transform.position, collider.transform.position);
|
||
|
||
// 更新最小距离和对应的目标
|
||
if (dis < minDistance)
|
||
{
|
||
minDistance = dis;
|
||
closestTarget = collider.gameObject;
|
||
}
|
||
}
|
||
|
||
if (closestTarget != null)
|
||
{
|
||
// 检查是否满足攻击间隔时间要求
|
||
if (Time.time - lastAttackTime < attackInterval) return;
|
||
|
||
//具体伤害
|
||
if (closestTarget.tag == "enemy")
|
||
{
|
||
FireWeapon(closestTarget.transform.position);
|
||
GoldFirePoint();
|
||
lastAttackTime = Time.time;
|
||
}
|
||
if (closestTarget.tag == "ore")
|
||
{
|
||
FireWeapon(closestTarget.transform.position);
|
||
GoldFirePoint();
|
||
lastAttackTime = Time.time;
|
||
}
|
||
}
|
||
}
|
||
//射击特效
|
||
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);
|
||
}
|
||
//导弹
|
||
void Firemissile()
|
||
{
|
||
// 创建导弹
|
||
GameObject missile = Instantiate(missilePrefab, bulletSpawn[2].position, bulletSpawn[2].rotation);
|
||
|
||
// 计算目标位置:沿着发射位置的 x 方向前进一定的距离
|
||
float xDistance =200f; // 设置导弹发射的距离,可以根据需要调整
|
||
Vector3 targetPosition = missile.transform.position + missile.transform.forward * xDistance; // 沿 x 轴发射
|
||
|
||
// 使用 DOMove 平滑地将导弹移动到目标位置
|
||
missile.transform.DOMove(targetPosition, 2f); // 2f是移动的时间,可以根据需要调整
|
||
}
|
||
|
||
//射击
|
||
void FireWeapon(Vector3 targetPosition)
|
||
{
|
||
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn[0].position, bulletSpawn[0].rotation);
|
||
GameObject bullet1 = Instantiate(bulletPrefab, bulletSpawn[1].position, bulletSpawn[1].rotation);
|
||
|
||
// 计算从武器到目标位置的方向
|
||
Vector3 direction = targetPosition - this.transform.position;
|
||
|
||
|
||
// 使武器朝着目标位置旋转
|
||
Quaternion rotation = Quaternion.LookRotation(direction);
|
||
bullet.transform.rotation = rotation;
|
||
bullet1.transform.rotation = rotation;
|
||
|
||
// 计算目标距离
|
||
float distance = Vector3.Distance(bulletSpawn[0].position, targetPosition);
|
||
float distance1 = Vector3.Distance(bulletSpawn[1].position, targetPosition);
|
||
|
||
// 根据距离设置移动时间(可以根据需要调整这个比例)
|
||
float moveDuration = Mathf.Clamp(distance / 20f, 0.2f, 0.7f); // 设定最小0.5秒,最大3秒
|
||
float moveDuration1 = Mathf.Clamp(distance1 / 20f, 0.2f, 0.7f); // 同理
|
||
|
||
|
||
float extensionDistance = 500f; // 延长的距离,可以根据需要调整
|
||
Vector3 extendedTargetPosition = targetPosition + direction.normalized * extensionDistance;
|
||
|
||
bullet.transform.DOMove(extendedTargetPosition, moveDuration);
|
||
bullet1.transform.DOMove(extendedTargetPosition, moveDuration1);
|
||
|
||
|
||
}
|
||
|
||
}
|