Spawn GameObject behind another GameObject



  • How do you spawn a GameObject behind another GameObject that's already moving? I'm trying to spawn this blue car behind the red one:
    alt text
    I want the blue car to follow the red one along the path and eventually add another car behind the blue one. Would anyone have a suggestion how this can be done?

    Thank you!



  • Thanks for registering here.

    You'll have two options for placing objects in this case:

    • at a waypoint behind the other object
    • at a percentual position

    For the first one, you would just read out the 'currentPoint' variable of the first object, subtract 1 and set it as the 'startPoint' variable of the second movement script.

    For the second option, I would recommend looking at the PathInput sample script for this, which is included in the package. There are some calculations involved, but basically that script sets the object using a progress value (range 0 - 1) on the path, where 0 is the start and 1 is 100% / the end. In the same way, you would read the path progress of the first object, subtract a small value like 0.02 (2%) and set it on the second.

    Hope this makes sense. :)



  • Baroni,

    That worked like a charm. It was much easier than I thought. I was doing all sorts of crazy things and didn't realize all I had to do was use those public variables. I'll take a look at the pathinput example as well.

    Thank you so much!



  • @Baroni

    So, I was able to get the second car to follow the first one when car is in the scene, but when I instantiate it, the blue car goes back to the beginning of the path. This is the script on the blue car:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using SWS;
    
    public class FollowCar : MonoBehaviour
    {
    
        public GameObject player;
        public splineMove followCar;
        public splineMove moveCar;
    
        // Start is called before the first frame update
        void Start()
        {
            player = GameObject.FindWithTag("Player");
            followCar = player.GetComponent<splineMove>();
            moveCar = GetComponent<splineMove>();
    
            Debug.Log("Spline Car " + moveCar.startPoint);
    
            moveCar.SetPath(WaypointManager.Paths["loop"]);
            moveCar.startPoint = followCar.currentPoint - 1;
            
        }
    
        // Update is called once per frame
        void Update()
        {
            //Debug.Log("Spline Car " );
            
        }
    
    }
    
    

    Any ideas why the startPoint isn't set after I instantiate it? I used moveCar.SetPath() to assign the path because I saw it in the instantiate example. Not sure that might be the issue.



  • @Baroni
    So, it turns out using the progress value sets the second car directly when you instantiate it. A much better approach after using SetPath().



  • SetPath() is starting the movement directly, which is why you would want to modify the startPoint beforehand. Glad that you figured out how to do it with the progress value as well.


Log in to reply