started building NPC combat
This commit is contained in:
@@ -17,18 +17,29 @@ public class Projectile : MonoBehaviour
|
||||
public Vector3 loadedPositionOffset = Vector3.zero;
|
||||
public Vector3 loadedRotationOffset = Vector3.zero;
|
||||
|
||||
[Tooltip("How strongly gravity affects the arrow. 1 = standard Unity gravity, 0 = flies perfectly straight.")]
|
||||
public float gravityScale = 1f;
|
||||
|
||||
private float _damage;
|
||||
private LayerMask _targetLayers;
|
||||
private bool _hasHit;
|
||||
private Vector3 _lastPosition;
|
||||
private Vector3 _startPosition;
|
||||
private Vector3 _velocity;
|
||||
|
||||
public void Initialize(float damage, LayerMask targets)
|
||||
public void Initialize(float damage, LayerMask targets, float powerMultiplier)
|
||||
{
|
||||
_damage = damage;
|
||||
// Damage is scaled based on how long the bow was drawn
|
||||
_damage = damage * powerMultiplier;
|
||||
_targetLayers = targets;
|
||||
|
||||
// Initial velocity is scaled by power multiplier
|
||||
_velocity = transform.forward * (speed * powerMultiplier);
|
||||
|
||||
// Destroy the arrow after a few seconds if it flies into the sky
|
||||
Destroy(gameObject, lifetime);
|
||||
|
||||
_startPosition = transform.position;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -40,14 +51,22 @@ public class Projectile : MonoBehaviour
|
||||
{
|
||||
if (_hasHit) return;
|
||||
|
||||
// Move the projectile forward
|
||||
Vector3 direction = transform.forward;
|
||||
float distance = speed * Time.deltaTime;
|
||||
transform.position += direction * distance;
|
||||
// Apply gravity to velocity over time (this creates the arc!)
|
||||
_velocity += Physics.gravity * gravityScale * Time.deltaTime;
|
||||
|
||||
// Make the arrow physically point in the direction it is travelling
|
||||
if (_velocity.sqrMagnitude > 0.01f)
|
||||
{
|
||||
transform.forward = _velocity.normalized;
|
||||
}
|
||||
|
||||
Vector3 direction = _velocity.normalized;
|
||||
float distance = _velocity.magnitude * Time.deltaTime;
|
||||
transform.position += _velocity * Time.deltaTime;
|
||||
|
||||
// Raycast from the previous position to the new position to detect hits
|
||||
// This prevents fast-moving arrows from passing straight through thin walls or enemies
|
||||
if (Physics.Raycast(_lastPosition, direction, out RaycastHit hit, distance, _targetLayers))
|
||||
// We explicitly Ignore triggers so arrows don't hit the giant Lock-On detection spheres!
|
||||
if (Physics.Raycast(_lastPosition, direction, out RaycastHit hit, distance, _targetLayers, QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
HandleHit(hit);
|
||||
}
|
||||
@@ -73,9 +92,12 @@ public class Projectile : MonoBehaviour
|
||||
Destroy(rb);
|
||||
}
|
||||
|
||||
// Deal damage
|
||||
Debug.Log($"Arrow hit {hit.collider.name} for {_damage} damage!");
|
||||
// Deal damage and log distance
|
||||
float distanceFlown = Vector3.Distance(_startPosition, transform.position);
|
||||
Debug.Log($"Arrow hit {hit.collider.name} for {_damage:F1} damage! It flew {distanceFlown:F1} meters.");
|
||||
hit.collider.GetComponent<WorldObject>()?.TakeDamage(_damage);
|
||||
hit.collider.GetComponent<NPCController>()?.TakeDamage(_damage);
|
||||
hit.collider.GetComponent<PlayerStats>()?.TakeDamage(_damage);
|
||||
|
||||
// Optional: Disable collider or script so it doesn't keep checking for hits
|
||||
Collider myCollider = GetComponent<Collider>();
|
||||
|
||||
Reference in New Issue
Block a user