Need help with this code? thanks.

void OnTriggerEnter(Collider player) {

	Debug.Log ("Press F to leave house");

	if (Input.GetKeyDown("f")) {

		transform.position = new Vector3(0,0,(180));

	
	}

}

}

so im new too coding, but im trying make it where if i click f my player then ends up on the other side of the door, i get no errors but it just simply wont work? if anyone knows i would appreciate the help thanks and happy new year.

In this current state, it is nearly impossible to press the key with adequate timing. Both the OnTriggerEnter() and Input.GetKeyDown() occur on a single frame of gameplay, and you would be hard pressed to be lucky enough to press the key at that exact moment.

A possible solution to this would be to use OnTriggerStay() instead. This triggers on every frame that the collider is within the Trigger’s boundaries.

Edit: Additionally, there might be some unusual logic to factor in as well. Based on the function you described, I’m guessing that script is attached to an area near the door and not to the player. Based on that assumption, rather than using transform.position to enter the area, you would want to use player.transform.position to move the object which moved into range of the trigger.

Hi, @demongames666

Maybe you should write Input.GetKeyDown(Keycode.F) not Input.GetKeyDown(“f”)

So this is your final code :
if (Input.GetKeyDown(KeyCode.F)) { transform.position = new Vector3(0,0,180f); }

Sorry for my bad english, if not working, you can ask or tell me again

  • Amar Fadil