You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

49 lines
1.3 KiB

using System;
using UnityEngine;
public class HomingBullet : BulletBase
{
[SerializeField] float rotSpeed = 100.0f;
[SerializeField] float factorByAngleDiff = 0f;
public Transform Target { get; private set; }
public bool IsHoming => Target != null;
public Action OnUpdate;
protected override bool UpdateBullet(float deltaTime)
{
if(Target is null) return false;
Vector2 toTarget = Target.position - transform.position;
float targetAngle = Utility.GetAngle2D(toTarget);
float currentAngle = Utility.GetAngle2D(HeadDirection);
float factor = 1.0f;
if(toTarget.sqrMagnitude < 3.0f * 3.0f)
{
factor = 0f;
}
if(factorByAngleDiff > 0)
{
factor *= Mathf.Abs(currentAngle - targetAngle) * factorByAngleDiff;
}
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0, 0, currentAngle), Quaternion.Euler(0, 0, targetAngle), rotSpeed * factor * deltaTime);
transform.position += (Vector3)HeadDirection * Speed * deltaTime;
return true;
}
public override void Fire(Transform target = null, bool resetLifeTime = true)
{
if (target is null) return;
base.Fire(target, resetLifeTime);
Target = target;
}
}