3DTD/Assets/Scripts/Tower/GravityTower.cs

29 lines
863 B
C#
Raw Normal View History

2024-04-20 19:22:52 +02:00
using UnityEngine;
public class GravityTower : Tower
{
[SerializeField, Range(0.1f, 10f)]
private float radius = 1f;
[SerializeField]
private Transform orb;
[SerializeField]
private LayerMask affectedBodies;
[SerializeField]
private AnimationCurve distToAcceleration;
protected override void FixedUpdate()
{
Collider[] projectilesInRadius = Physics.OverlapSphere(orb.position, radius, affectedBodies);
foreach (var projectile in projectilesInRadius)
{
Rigidbody projectileBody = projectile.GetComponent<Rigidbody>();
Vector3 projToOrb = orb.position - projectile.transform.position;
float dist = projToOrb.magnitude;
projectileBody.AddForce(projToOrb.normalized * distToAcceleration.Evaluate(dist));
}
}
}