I’m making a game where depending on a Button-Press some Objects exist, and others don’t. The problem is that some of these Objects are moving platforms so I need to make the Player a Child of the Object for these, and therefore when I deactivate these Objects i also deactivate the Player. Any way to avoid that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Click : MonoBehaviour
{
public GameObject red;
public GameObject blue;
bool CubeExists = true;
void Start()
{
//blue.transform.localScale = new Vector3(0, 0, 0);
blue.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (CubeExists)
{
Debug.Log("MouseClick was pressed.");
//blue.transform.localScale = new Vector3(0, 0, 0);
//red.transform.localScale = new Vector3(1, 1, 1);
blue.SetActive(true);
red.SetActive(false);
CubeExists = false;
}
else
{
Debug.Log("MouseClick was double pressed.");
//blue.transform.localScale = new Vector3(1, 1, 1);
//red.transform.localScale = new Vector3(0, 0, 0);
blue.SetActive(false);
red.SetActive(true);
CubeExists = true;
}
}
}
}
Instead of putting the player as a child, simulate that effect in Update() basically using player.transform.x = player.transform.x - oldplat.x + platform.transform.x;
So basically the platform moves, the player moves too. Simulating the player being a child to the platform. Little inefficient, but unless the performance is super top priority, (each milsec counts) then that should be fine.
If the player is a child of the object and you have script on the player (maybe called PlayerController). Just before you deactivate the object you could call something like this.
// Check if there is a player attached to the object
var player = objToDeactivate.GetComponentInChildren<PlayerController>();
if (player != null) {
// Detach player from the object
player.transform.SetParent(null);
}
Then in the chance you have more then one player in the scene you could use the GetComponentsInChildren to get an array of all players attached instead of just one.
using UnityEngine;
public class GameManager : MonoBehaviour
{
[SerializeField]
private GameObject player;
[SerializeField]
private GameObject firstPlatform;
[SerializeField]
private GameObject secondPlatform;
private void Awake()
{
SetPlayerAndPlatformsToDefault();
}
private void Update()
{
if (!Input.GetMouseButtonDown(0)) return;
MovePlayerToNextPlatform();
SetNextPlatformAsActive();
}
private void SetPlayerAndPlatformsToDefault()
{
player.transform.parent = firstPlatform.transform;
firstPlatform.SetActive(true);
secondPlatform.SetActive(false);
}
private void MovePlayerToNextPlatform()
{
player.transform.parent = secondPlatform.activeSelf ?
firstPlatform.transform :
secondPlatform.transform;
}
private void SetNextPlatformAsActive()
{
secondPlatform.SetActive(!secondPlatform.activeSelf);
firstPlatform.SetActive(!firstPlatform.activeSelf);
}
}
@montymomentum_unity One way would be to change the player's parent before deactivating the platform they are attached to.