Search Unity

Question In Unity object from ObjectPool appears with rotation of last appeared object

Discussion in 'Scripting' started by Kujji, Mar 19, 2023.

  1. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    In my game i have ship that shoots lasers, i made shooting using ObectPool, but got stuck with lasers wrong direction. I make them move by adding RelativeForce, but they move sideways forward. I tryed to instantiate objects with _placeToSpawn.transform instead of its position, but instantiated object are childs of Ship and when ship moves lasers move too.
    How to set force direction of activated laser right?
    Video of wrong moving lasers:


    ObjectPool code:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System.Linq;
    4.  
    5. public class ObjectPool : MonoBehaviour
    6. {
    7.     [SerializeField] private GameObject _placeToSpawn;
    8.     [SerializeField] private int _amount;
    9.  
    10.     private List<GameObject> _pool = new();
    11.  
    12.     protected void Initialize(GameObject prefab)
    13.     {
    14.         for (int i = 0; i < _amount; i++)
    15.         {
    16.             GameObject spawned = Instantiate(prefab, _placeToSpawn.transform.position, Quaternion.identity);
    17.             spawned.SetActive(false);
    18.  
    19.             _pool.Add(spawned);
    20.         }
    21.     }
    22.  
    23.     protected bool TryGetObject(out GameObject result)
    24.     {
    25.         result = _pool.FirstOrDefault(gameObject => gameObject.activeSelf == false);
    26.  
    27.         return result != null;
    28.     }
    29. }
    Ship shooting objects from ObjectPool code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4. public class ShipShooter : ObjectPool
    5. {
    6.     [SerializeField] private UIdata _score;
    7.     [SerializeField] private GameObject _laser;
    8.     [SerializeField] private AudioClip _shootAudio;
    9.     [SerializeField] private AudioSource _indestructibleSource;
    10.     [SerializeField] private float _laserSpeed;
    11.     [SerializeField] private float _laserShootVolume;
    12.  
    13.     private AudioSource _source;
    14.     private bool _laserShooted = false;
    15.  
    16.     private void Start()
    17.     {
    18.         Initialize(_laser);
    19.         _source = GetComponent<AudioSource>();
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.Space))
    25.         {
    26.             CreateLaser();
    27.         }
    28.     }
    29.  
    30.     private void CreateLaser()
    31.     {
    32.         if (TryGetObject(out GameObject laser))
    33.         {
    34.             SetLaser(laser);
    35.         }
    36.  
    37.     }
    38.  
    39.     private void SetLaser(GameObject laser)
    40.     {
    41.         laser.SetActive(true);
    42.  
    43.         laser.transform.SetPositionAndRotation(transform.position, transform.rotation);
    44.         laser.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * _laserSpeed);
    45.         laser.GetComponent<Laser>().score = _score;
    46.         laser.GetComponent<Laser>().source = _indestructibleSource;
    47.         _source.PlayOneShot(_shootAudio, _laserShootVolume);
    48.         _laserShooted = false;
    49.     }
    50. }
     
    Last edited: Mar 19, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Nothing magic here... the same simple steps to success:

    - get the laser object
    - compute the direction you want it to face
    - set that direction

    This also may just be another example of the extremely high costs and issues associated with object pooling / pools.

    Good luck debugging!

    https://forum.unity.com/threads/object-pooling.1329729/#post-8405055

    https://forum.unity.com/threads/object-pooling-in-a-tower-defense-game.1076897/#post-6945089
     
    orionsyndrome likes this.
  3. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    I did it in line 43, but rotation didn't change for current laser
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    Why don't you properly debug and check the value before and after, and try to understand what went wrong?
    You can very easily trace the execution and re-check your assumptions.
     
    Kurt-Dekker and Yoreki like this.
  5. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    Worked with this line code instead of 44 line:

    laser.GetComponent<Rigidbody2D>().AddForce(transform.up * _laserSpeed);