- Home /
How to create detonation time for grenade projectile.
Hello everyone, I have problems with setting the time of detonation and destruction of the grenade projectile. Since the enemy creates grenades with a cooldown that is shorter than the detonation time, as you might guess, the next instance of the projectile will set the default detonation time. Luckily, I was able to fix this problem by setting the detonation time in the launch function, but with this approach, when the detonation time reaches 0, only the last grenade created will be destroyed. Any help is appreciated! Code, it's a little messy, but you need the start and fixUpdate functions:
private AttackDetails attackDetails;
public GameObject player;
[Tooltip("Position we want to hit")]
public Vector3 targetPos;
[Tooltip("Horizontal speed, in units/sec")]
public float speed = 25;
[Tooltip("How high the arc should be, in units")]
public float arcHeight = 6;
private Vector3 nextPos;
PlayerMovement playerMovement;
Rigidbody2D rb;
private bool arrowHitPlayerPos = false;
Vector3 startPos;
private bool hasHitGround;
Vector2 velocityVector;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private LayerMask whatIsPlayer;
[SerializeField]
private Transform damagePosition;
[SerializeField]
private float damageRadius;
private float timeOfDetonation = 3;
private float explosionRadius = 2;
Collider2D damageHit;
void Start() {
// Cache our start position, which is really the only thing we need
// (in addition to our current position, and the target).
startPos = transform.position;
playerMovement = FindObjectOfType<PlayerMovement>();
rb = GetComponent<Rigidbody2D>();
targetPos = playerMovement.groundPlayerPosVector;
timeOfDetonation = 3;
Debug.Log(timeOfDetonation);
}
void Update() {
// Compute the next position, with arc added in
Debug.Log(timeOfDetonation);
float x0 = startPos.x;
float x1 = targetPos.x;
float dist = x1 - x0;
float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime);
float baseY = Mathf.Lerp(startPos.y, targetPos.y, (nextX - x0) / dist);
float arc = arcHeight * (nextX - x0) * (nextX - x1) / (-0.25f * dist * dist);
nextPos = new Vector3(nextX, baseY + arc, transform.position.z);
velocityVector = nextPos - transform.position;
timeOfDetonation -= Time.deltaTime;
// Rotate to face the next position, and then move there
transform.rotation = LookAt2D(nextPos - transform.position);
if(hasHitGround != true || rb.velocity.y <=0)
rb.velocity = velocityVector * speed;
}
private void FixedUpdate()
{
if (!hasHitGround)
{
if(timeOfDetonation <=0)
{
Collider2D damageHit = Physics2D.OverlapCircle(damagePosition.position, explosionRadius, whatIsPlayer);
Destroy(gameObject);
}
Collider2D groundHit = Physics2D.OverlapCircle(damagePosition.position, damageRadius, whatIsGround);
if (damageHit)
{
damageHit.transform.SendMessage("Damage", attackDetails);
}
if (groundHit)
{
hasHitGround = true;
}
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(damagePosition.position, damageRadius);
Gizmos.DrawWireSphere(damagePosition.position, explosionRadius);
}
///
/// This is a 2D version of Quaternion.LookAt; it returns a quaternion
/// that makes the local +X axis point in the given forward direction.
///
/// forward direction
/// Quaternion that rotates +X to align with forward
static Quaternion LookAt2D(Vector2 forward) {
return Quaternion.Euler(0, 0, Mathf.Atan2(forward.y, forward.x) * Mathf.Rad2Deg);
}
Your answer
Follow this Question
Related Questions
How do I destroy the object that was instatiated 1 Answer
How to make projectiles shoot diagonally? 1 Answer
Remeber the value between frame Update() 1 Answer
Instantiate does not return 3 Answers