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

65 lines
1.5 KiB
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;
2024-04-21 03:03:15 +02:00
[SerializeField]
private string audioName;
2024-04-21 03:10:37 +02:00
private AudioSource audioSc;
2024-04-21 03:03:15 +02:00
2024-04-20 22:42:06 +02:00
public override void TowerSelected(bool selected)
{
}
protected override void Awake()
{
base.Awake();
}
2024-04-21 03:03:15 +02:00
protected override void Start()
{
base.Start();
if (!string.IsNullOrEmpty(audioName))
{
2024-04-21 03:10:37 +02:00
audioSc = AudioManager.PlaySound(audioName, transform.position, false, true, true);
2024-04-21 03:03:15 +02:00
}
}
2024-04-20 19:22:52 +02:00
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));
}
}
2024-04-20 22:42:06 +02:00
protected override void OnDestroy()
{
base.OnDestroy();
2024-04-21 03:10:37 +02:00
audioSc.Stop();
2024-04-20 22:42:06 +02:00
}
protected override void Update()
{
base.Update();
}
2024-04-20 19:22:52 +02:00
}