KeyCode not detected when OnTriggerStay = true
Hello everyone, Im running into an issue with my code. I want the InTheCar bool to become true when you are in the trigger and press E. For so far this function is doing nothing for me.
Thanks so much for looking into this.
public bool InTheCar = false;
void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Debug.Log("Enter Car");
if (Input.GetKey(KeyCode.E))
{
InTheCar = true;
other.transform.SetParent(this.transform);
}
if (Input.GetKey(KeyCode.E) && InTheCar == true)
{
other.transform.SetParent(this.transform, false);
InTheCar = false;
}
if (InTheCar == true)
{
other.GetComponent<Movement>().enabled = false;
}
else
{
other.GetComponent<Movement>().enabled = true;
}
}
}
void OnTriggerExit(Collider other)
{
Debug.Log("No Car Nearby");
}
Comment
Best Answer
Answer by Hellium · Dec 02, 2021 at 01:44 PM
void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Debug.Log("Enter Car");
if (Input.GetKeyDown(KeyCode.E))
{
if(InTheCar)
LeaveCar(other.GetComponent<Movement>());
else
EnterCar(other.GetComponent<Movement>());
}
}
}
void LeaveCar(Movement player)
{
player.enabled = true;
player.transform.SetParent(null);
InTheCar = false;
}
void EnterCar(Movement player)
{
player.enabled = false;
player.transform.SetParent(transform);
InTheCar = true;
}
Your answer
Follow this Question
Related Questions
Change Scene with Physics Raycasting 1 Answer
Player Can Not get off platform 1 Answer
How to get the Collider other element Rigidbody? 0 Answers
touch to activate animation 0 Answers