Hi, for my game I am attempting to make a car enter/ exit script. I’m really confused on how I would set my player to be a child of the object it is colliding with. This is what I have so far. Thank you so much!
public class CarSystem : MonoBehaviour {
public Transform player;
private GameObject car;
void Start()
{
}
void OnTriggerEnter(Collider other) {
if (Input.GetKey ("e") && other.gameObject.tag == "Car")
{
Debug.Log("Entered the car!");
//set first person controller to child of car
//set first person controller position to 0,0,0
}
Parent is a veriable in gameobjects so you would just set it to what ever you want
so say we have the player and you want to parent it to car and for i will randomly choose that the script is on the car
player.transform.parent = this.gameObject
now the player’s parent is the gameobject that the script is attached to
To un-parent you set the parent to null
player.transform.parent = null;
now the player has no parent
so with your code, you work from the collider of the car i assume so it would become
void OnTriggerEnter(Collider other) {
if (Input.GetKey ("e") && other.gameObject.tag == "Car")
{
Debug.Log("Entered the car!");
//assuming other is a collider attached to the
//car as it is tagged
this.transform.parent = other.transform;
//set first person controller to child of car
//set first person controller position to 0,0,0
}