Search code examples
c#unity-game-enginepositiontransform

Unity3D position Update when gameObject is changed


I am trying to make a vehicle changing game where there are 3 gameObject: car, tank and hover and if I press a button to change from car to a tank/hover i want them to be in a position where the car was.

the z axis is forward

I tried this code below but when I go backwards this will not work

UnityEngine;

public class Carswitcer : MonoBehaviour
{
public Transform car;
public Transform hover;
public Transform tank;

void Start()
{
    car.gameObject.SetActive(true);
    tank.gameObject.SetActive(false);
    hover.gameObject.SetActive(false);
}


public void Car()
    {
    
        if (car.position.z < hover.position.z)
            {
                car.position = hover.position;
            }
        if (car.position.z < tank.position.z)
            {
                car.position = tank.position;
            }

        car.gameObject.SetActive(true);
        tank.gameObject.SetActive(false);
        hover.gameObject.SetActive(false);
    }


public void Tank()
    {

        if (tank.position.z < hover.position.z)
            {
                tank.position = hover.position;
            }
        if (tank.position.z < car.position.z)
            {
                tank.position = car.position;
            }
        car.gameObject.SetActive(false);
        tank.gameObject.SetActive(true);
        hover.gameObject.SetActive(false);
    }



public void Hover()
    {

        if (hover.position.z < car.position.z)
            {
                hover.position = car.position;
            }
        if (hover.position.z < tank.position.z)
            {
                hover.position = tank.position;
            }

        car.gameObject.SetActive(false);
        tank.gameObject.SetActive(false);
        hover.gameObject.SetActive(true);

    }   
      

Solution

  • Why compare the z at all? Simply always copy the position of the currently active vehicle:

    public class Carswitcer : MonoBehaviour
    {
        public Transform car;
        public Transform hover;
        public Transform tank;
    
        // Store the current vehicle
        private Transform currentVehicle;
    
        void Start()
        {
            tank.gameObject.SetActive(false);
            hover.gameObject.SetActive(false);
    
            SetActiveVehicle(car);           
        }
    
        private void SetActiveVehicle(Transform newActiveVehicle)
        {
            // Is there a current vehicle?
            if(currentVehicle)
            {
                // If so copy its position and set it inactive
                newActiveVehicle.position = currentVehicle.position;
                currentVehicle.gameObject.SetActive(false);
            }
    
            // Store the new active reference and set it active
            currentVehicle = newActiveVehicle;
            currentVehicle.gameObject.SetActive(true);
        }
    
        public void Car()
        {
            SetActiveVehicle(car);
        }
    
    
        public void Tank()
        {
            SetActiveVehicle(tank);
        }
    
        public void Hover()
        {        
            SetActiveVehicle(hover);
        }