1
\$\begingroup\$

I am working on a 2D side scroller. I have created an enemy character that shoots at my player randomly within a specific time interval. So, I want to deflect the projectiles back toward the enemy after my player attacks the bullet. I looked at a different forum where someone suggested retaining the position from which it originates and applying a velocity in that direction from the player. I tried. I have added different destruction times to the bullet if it hits the player or is deflected. The projectiles take 3s more to destroy on deflection (runs correctly). Still, they don't move after being hit! This leads me to believe there is a problem with my ReturnToStartPosition() function below. I would truly appreciate any help!

  • The bullets are being instantiated from a muzzle point attached to a Bandit object. The bandit script handles instantiating the bullets. (I have tried to model my ReturnToStartPosition() similar to the way the bullets are shot in the bandit object. The bullet instantiation code is not my own)

  • I am using a diamond sprite as a prefab with this code attached

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Bullet : MonoBehaviour
    {
        public float destructionDelay = 1.5f; // Delay before destroying the bullet after collision
        private bool beenDeflected = false;
        private Rigidbody2D rb;
        private Bandit bandit; // Reference to the Bandit instance that shot the bullet
    
        private void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Method to set the Bandit reference when the bullet is shot
        public void SetShooter(Bandit shooter)
        {
            bandit = shooter;
        }
    
        private void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.gameObject.CompareTag("Player") && !beenDeflected)
            {
    
               StartCoroutine(DestroyBulletAfterDelay(destructionDelay));
            }
            else if (collision.gameObject.CompareTag("Player") && beenDeflected)
            {
    
    ReturnToStartPosition(); 
               StartCoroutine(DestroyBulletAfterDelay(destructionDelay+3f));
            }
        }
    
        private IEnumerator DestroyBulletAfterDelay(float delay)
        {
            yield return new WaitForSeconds(delay);
            Destroy(gameObject);
        }
    
        public void BeenDeflected()
        {
            beenDeflected = true;
        }
    
        private void ReturnToStartPosition()
        {
            Transform deflectionPoint = FindObjectOfType<PlayerController>().transform;
            Transform returnPoint = FindObjectOfType<Bandit>().transform;
    
            if (bandit != null)
            {
                rb.velocity = Vector2.zero;
                float shotPower = bandit.ShotPower();
                Vector2 myPos = new Vector2(transform.position.x, transform.position.y);
                Vector2 returnPos = new Vector2(returnPoint.position.x, returnPoint.position.y);
                Vector2 direction = returnPos - myPos; // Corrected calculation
                rb.velocity = direction.normalized * shotPower;
            }
        }
    }
    
\$\endgroup\$
2
  • \$\begingroup\$ Hi, thank you for the detailed question! Could you additionally log (Debug.Log) the values shotPower, myPos, returnPos, direction and rb.velocity in ReturnToStartPosition() to see where the code fails exactly? If everything is correct, then I assume there is some different problem later on that stops the movement, maybe a collision. \$\endgroup\$
    – Cebbi
    Commented May 19, 2023 at 7:05
  • \$\begingroup\$ Thank you so much!!! After logging everything in the ReturnToStartPosition(), I realized the function wasn't being called because bandit == null in the prefab context because of the way I was trying to declare it. I used Bandit bandit = FindObjectOfType<Bandit>(); and it worked beautifully. Thank you @Cebbi! \$\endgroup\$
    – shanksVR
    Commented May 19, 2023 at 8:06

0

You must log in to answer this question.

Browse other questions tagged .