using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
public class BulletLogic : MonoBehaviour
{
public float speed = 1f;
public float maxDistance = 50;
private Vector3 birthPosition;
public Transform targetTransform;
private Vector3 cPoint;
private float percentSpeed;
private float percent;
void Start()
{
Application.targetFrameRate = 60;
birthPosition = transform.position;
Vector3 dir = targetTransform.position - transform.position;
percentSpeed = speed / dir.magnitude;
cPoint = new Vector3(Random.Range(-1f, 1f), Random.Range(0f, 1f), Random.Range(-1f, 1f));
cPoint.Normalize();
float rand = Random.Range(0.5f, 0.7f);
cPoint = transform.position + dir * rand + cPoint * dir.magnitude * rand * Random.Range(0.7f, 1f);
float destroyTime = maxDistance / speed;
Invoke("SelfDestroy", destroyTime);
}
void Update()
{
percent += percentSpeed * Time.deltaTime;
percent = Mathf.Clamp01(percent);
transform.position = Bezier(birthPosition, targetTransform.position, cPoint, percent);
}
private Vector3 Bezier (Vector3 start, Vector3 target, Vector3 mid, float t)
{
Vector3 p1 = Vector3.Lerp(start, mid, t);
Vector3 p2 = Vector3.Lerp(mid, target, t);
return Vector3.Lerp(p1, p2, t);
}
void SelfDestroy ()
{
Object.Destroy(gameObject);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】