Whats wrong in my code or logic, please help!,Hi everyone, i have a problem with a door key situation:
I want to collect a key and when the player collects the key, the player should be able to open a door ontrigger enter.
here is my script
public class Key : MonoBehaviour {
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Player player = other.GetComponent<Player>();
player.CollectKey();
player.OpenDoor();
Destroy(this.gameObject);
}
}
}
Player script:
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Lava")
Destroy(this.gameObject);
if (other.tag == "Door" && _canOpenDoor == true)
{
Debug.Log("Hello");
Destroy(other.gameObject);
}
}
public void Shoot()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(bulletPrefab, new Vector3(transform.position.x +1, transform.position.y, 0), Quaternion.identity);
}
}
public void CollectKey()
{
keyIsCollected = true;
uimanager.UpdateCoins(keyIsCollected);
}
public void OpenDoor()
{
_canOpenDoor = true;
}
},
The Player has trigger enabled The Door has trigger disabled
if i enable both triggers it works in the way that the door gets destroyed, but if i only enable the trigger on the player it seems i can't collide ?
Also, i use a charactercontroller on my player and none rigidbody the door has a box collider and a rigidbody
Answer by daniblue92 · Jun 12, 2020 at 08:54 PM
I solved it, the problem was the difference in collisiondetection as rigidbodies to colliders and charactercontroller to colliders.
for charactercontroller neither ontriggerenter or oncollisionenter works instead you use the oncontrollercollider hit function.. its hard for a absolute beginner to even understand whats the real problem i guess
Your answer
Follow this Question
Related Questions
Using Multiple Activated Triggers to trigger a Separate Event. 0 Answers
how do i make an object make itself and another object disapear when touched (2d game, 3d unity) 0 Answers
Trigger when holding key 1 Answer
HubDoor in 2D Game kit- Trying to make a door open after collecting 3 items 1 Answer
How can I make the Player be destroyed when I touch the end of the camera? 1 Answer