Vehicle enter/exit, why is my trigger collider not working?
Okay, so I am making a game where you run around space and do stuff in first person. You have the ability to get in a spaceship and explore. I am trying to create a script that allows the player to enter and exit the spaceship, however when my player enters the box collider nothing happens. no GUI to enter the vehicle pops up and pressing the key assigned to enter the vehicle does not do anything. Can some one please help?
Script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CharacterManager : MonoBehaviour
{
private bool inStarship = false;
StarshipManager starshipManager;
public GameObject guiObj;
GameObject player;
void Start()
{
starshipManager = GetComponent<StarshipManager>();
player = GameObject.FindWithTag("Player");
guiObj.SetActive(false);
}
// Update is called once per frame
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Player" && inStarship == false)
{
Debug.Log("player has entered trigger");
guiObj.SetActive(true);
if (Input.GetKey(KeyCode.F))
{
Debug.Log("F has been pressed");
guiObj.SetActive(false);
player.transform.parent = this.gameObject.transform;
starshipManager.enabled = true;
player.SetActive(false);
inStarship = true;
}
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
guiObj.SetActive(false);
}
}
void Update()
{
if (inStarship == true && Input.GetKey(KeyCode.F))
{
starshipManager.enabled = false;
player.SetActive(true);
player.transform.parent = null;
inStarship = false;
}
}
}
Screenshots:
Answer by xxmariofer · Jul 17, 2020 at 08:12 AM
Hello, your issue is with the tags, the script is attached to the Player, so in the trigger event you need to compare the tag with the Starship, not the player (cant collide with itself), remember to add the tag starship to the ship in the inspector before trying
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Starship" && inStarship == false)
Thank you so much. I feel so dumb, I was attaching the script to my player instead of the Starship.