Search Unity

Resolved The movement of one prefab imposes on its clone

Discussion in 'Scripting' started by nonntori, May 17, 2023.

  1. nonntori

    nonntori

    Joined:
    Jun 21, 2022
    Posts:
    2
    Hi all, ran into this problem. When quickly creating a prefab, the movement overlaps each other. After googling I could not find a solution. If anyone has encountered this problem and can tell me how to solve it, I would be grateful for help.
    Unity - 2021.3.14f1

    Video demonstration:


    Movement code:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class MissileMovement : MonoBehaviour
    5. {
    6.     [SerializeField] private BoxCollider _pointRadius1;
    7.     [SerializeField] private BoxCollider _pointRadius2;
    8.    
    9.     private Vector3 _intermediatePoint1;
    10.     private Vector3 _intermediatePoint2;
    11.  
    12.     private const float FinalPosition = 1f;
    13.     private readonly float _scaleCollider = 2f;
    14.  
    15.     public IEnumerator MoveUp(Vector3 startPosition, Vector3 endPosition, float position, GameObject missile, float speed)
    16.     {
    17.         _intermediatePoint2 = GetRandomPointInBounds(_pointRadius2, _scaleCollider);
    18.  
    19.         while (FinalPosition != position)
    20.         {
    21.             position += speed * Time.deltaTime;
    22.  
    23.             missile.transform.position = GetTrajectoryMove(startPosition, _intermediatePoint2,endPosition, position);
    24.             missile.transform.rotation = GetRotation(startPosition, _intermediatePoint2,endPosition, position);
    25.  
    26.             yield return null;
    27.         }
    28.     }
    29.    
    30.     public IEnumerator MoveDown(Vector3 startPosition, Vector3 endPosition, float position, GameObject missile, float speed)
    31.     {
    32.         _intermediatePoint1 = GetRandomPointInBounds(_pointRadius1, _scaleCollider);
    33.  
    34.         while (FinalPosition != position)
    35.         {
    36.             yield return null;
    37.  
    38.             position += speed * Time.deltaTime;
    39.            
    40.             missile.transform.position = GetTrajectoryMove(startPosition, _intermediatePoint1, endPosition, position);
    41.             missile.transform.rotation = GetRotation(startPosition, _intermediatePoint1,endPosition, position);
    42.         }
    43.     }
    44.  
    45.     private Quaternion GetRotation(Vector3 startPosition,Vector3 intermediatePoint, Vector3 endPosition, float position)
    46.     {
    47.         Quaternion rotation = Quaternion.LookRotation(CurveBizier.GetFirstDerivative(
    48.             startPosition,
    49.             intermediatePoint,
    50.             endPosition,
    51.             position));
    52.  
    53.         return rotation;
    54.     }
    55.    
    56.     private Vector3 GetTrajectoryMove(Vector3 startPosition,Vector3 intermediatePoint, Vector3 endPosition, float position)
    57.     {
    58.         Vector3 trajector = CurveBizier.GetPoint(
    59.             startPosition,
    60.             intermediatePoint,
    61.             endPosition,
    62.             position);
    63.  
    64.         return trajector;
    65.     }
    66.  
    67.     private Vector3 GetRandomPointInBounds(Collider collider, float scale)
    68.     {
    69.         Bounds bounds = collider.bounds;
    70.         Vector3 point = new Vector3(
    71.             Random.Range(bounds.min.x * scale, bounds.max.x * scale),
    72.             Random.Range(bounds.min.y * scale, bounds.max.y * scale),
    73.             Random.Range(bounds.min.z * scale, bounds.max.z * scale));
    74.  
    75.         return point;
    76.     }
    77. }
    Prefab creation code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(MissileMovement))]
    4. public class Missile : MonoBehaviour
    5. {
    6.     [SerializeField] private float _speed;
    7.     [SerializeField] private GameObject _missilePrefab;
    8.     [SerializeField] private Camera _mainCamera;
    9.    
    10.     private MissileMovement _movement;
    11.     private float _position = 0;
    12.  
    13.     private void Start()
    14.     {
    15.         _movement = GetComponent<MissileMovement>();
    16.     }
    17.  
    18.     public void Launch(Vector3 mousePosition)
    19.     {
    20.         Ray ray = _mainCamera.ScreenPointToRay(mousePosition);
    21.  
    22.         if (Physics.Raycast(ray, out RaycastHit hit))
    23.         {
    24.             var position = _mainCamera.transform.position;
    25.  
    26.             if (hit.collider && hit.point.y <= Vector3.down.y)
    27.             {
    28.                 GameObject missile1 = Instantiate(_missilePrefab, position, Quaternion.identity);
    29.                 StartCoroutine(_movement.MoveDown(position, hit.point, _position, missile1, _speed));
    30.             }
    31.            
    32.             if(hit.collider && hit.point.y >= Vector3.up.y)
    33.             {
    34.                 GameObject missile2 = Instantiate(_missilePrefab, position, Quaternion.identity);
    35.                 StartCoroutine(_movement.MoveUp(position, hit.point, _position, missile2, _speed));
    36.             }
    37.         }
    38.     }
    39. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Line 29 and Line 35 above operate on
    _movement
    , your prefab.

    You need to operate on the instance of MissileMovement that you just spawned.
     
    spiney199 likes this.
  3. nonntori

    nonntori

    Joined:
    Jun 21, 2022
    Posts:
    2
    Thank you so much for the advice, now everything works as I wanted it to.
     
    Last edited: May 18, 2023
    Kurt-Dekker likes this.