Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Chasing Rocket

Discussion in 'Open Projects' started by AstralPL, May 10, 2023.

  1. AstralPL


    May 10, 2023
    Hi! I am looking for help with my Unity project. The character is to be chased by rockets, they are supposed to first appear above the board, then move towards the character, and when they are close, select the nearest "overlapping field" on the grid, hit it and explode. Destroying the character if standing there or on the nearest adjacent overlaps. How to do it? How to add an explosion animation? How to create an area representing the rocket's firing area, increasing as it approaches?
  2. jgone


    May 14, 2021
    If the surface is always a plane, then the nearest field is the one directly under the rocket at the moment you've decided the player and the rocket are close enough to each other. You can then change the rocket's trajectory to move towards the center of that field rather than the player. The area representing the fields adjacent to impact field is a 3x3 two-dimensional square centered at the impact field. Work out the coordinates that represent the boundaries of that 3x3 square and check if the player's x and y coordinates are within that square. I have never messed around with animation so can't offer help there.
  3. Naruto45me


    Feb 26, 2017
    To implement the chasing rockets behavior and explosion animation in Unity, you can follow these steps:

    1. Create the Rocket Prefab:
      • Design a rocket model or find one in the Unity Asset Store.
      • Import the rocket model into Unity and create a prefab from it.
      • Add a Rigidbody component to the rocket prefab to enable physics simulation.
    2. Spawn Rockets:
      • Instantiate rocket prefabs above the game board at random positions.
      • Make sure to set the rocket's initial movement direction towards the player character.
    3. Movement:
      • Update the rocket's position in the FixedUpdate() method by applying a force or directly changing its velocity towards the player character.
      • You can use Unity's Physics.Raycast() or similar methods to check if the rocket is about to hit any obstacles or the character.
    4. Grid and Overlapping Fields:
      • Create a grid system to represent the game board and its fields.
      • Each field can be represented as a separate GameObject with its own Collider component.
      • You can use Unity's Physics.OverlapBox() or Physics.OverlapSphere() to detect the nearest overlapping fields for the rocket's explosion.
    5. Explosion Animation:
      • Create an explosion animation using particle effects or sprite animations.
      • Add the explosion animation to the rocket prefab.
      • When the rocket hits an overlapping field, instantiate the explosion animation at that position and play it.
    6. Destroy Character and Adjacent Fields:
      • When the explosion animation is triggered, check if the character is standing on the overlapping field.
      • If the character is on the overlapping field, destroy the character GameObject.
      • Also, check for adjacent overlapping fields and destroy them if they exist.
    7. Firing Area:
      • To create an area representing the rocket's firing area, you can use a transparent or semi-transparent texture or a separate mesh around the rocket.
      • Adjust the size of this area based on the rocket's proximity to the character.
      • You can scale the area representation using Unity's Transform.localScale or adjust its radius for a circular representation.
    Remember to consider performance optimization, such as using object pooling to manage rocket instantiation, and adding appropriate tags or layers for efficient collision detection.

    Code (CSharp):
    1. using UnityEngine;
    3. public class Rocket : MonoBehaviour
    4. {
    5.     public Transform playerCharacter;
    6.     public float speed = 5f;
    7.     public float explosionRadius = 2f;
    8.     public GameObject explosionPrefab;
    10.     private Rigidbody rb;
    12.     private void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody>();
    15.     }
    17.     private void FixedUpdate()
    18.     {
    19.         // Calculate the direction towards the player character
    20.         Vector3 direction = (playerCharacter.position - transform.position).normalized;
    22.         // Apply force to move the rocket towards the player character
    23.         rb.AddForce(direction * speed, ForceMode.VelocityChange);
    24.     }
    26.     private void OnCollisionEnter(Collision collision)
    27.     {
    28.         // Check if the rocket collided with an overlapping field
    29.         if (collision.collider.CompareTag("OverlappingField"))
    30.         {
    31.             // Instantiate explosion prefab at the collision point
    32.             Instantiate(explosionPrefab, collision.GetContact(0).point, Quaternion.identity);
    34.             // Check if the player character is standing on the overlapping field
    35.             if (collision.collider.bounds.Contains(playerCharacter.position))
    36.             {
    37.                 // Destroy the player character
    38.                 Destroy(playerCharacter.gameObject);
    39.             }
    41.             // Check for adjacent overlapping fields within explosion radius
    42.             Collider[] hitColliders = Physics.OverlapSphere(collision.collider.transform.position, explosionRadius);
    43.             foreach (Collider hitCollider in hitColliders)
    44.             {
    45.                 if (hitCollider.CompareTag("OverlappingField"))
    46.                 {
    47.                     // Destroy the adjacent overlapping fields
    48.                     Destroy(hitCollider.gameObject);
    49.                 }
    50.             }
    52.             // Destroy the rocket
    53.             Destroy(gameObject);
    54.         }
    55.     }
    56. }
    Make sure to attach this script to your Rocket prefab. Also, assign the playerCharacter reference, speed, explosionRadius, and explosionPrefab variables in the Inspector.

    Remember to create a separate script for spawning rockets, handle the grid system, and set up the explosion animation prefab with appropriate particle effects or sprite animations.