Im trying to write a code where if the player is in a collider and presses E, they are teleported somewhere else. I know the collider works but I’m not sure about the E and the teleport. I tried to debug log but it didn’t work. the problems are at lines 17-19.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class doorscript : MonoBehaviour
{
[SerializeField] private Animator myAnimationController;
public AudioSource audio;
public AudioClip clip1;
public AudioClip clip2;
public AudioClip clip3;
private void OnTriggerEnter2D(Collider2D col){
if (col.CompareTag("Player")){
myAnimationController.SetBool("open", true);
audio.PlayOneShot(clip1);
if (Input.GetKeyDown(KeyCode.E)){
Debug.Log("ya clicked e");
transform.position = new Vector2(180, 1.38f);
audio.PlayOneShot(clip3);
}
}
}
private void OnTriggerExit2D(Collider2D col){
if (col.CompareTag("Player")){
myAnimationController.SetBool("open", false);
audio.PlayOneShot(clip2);
}
}
}
show that I was pressing E but I don’t know where I could have gone wrong.
In this scenerio, you would have to hit the “e” key at the very precise moment that you entered the trigger. Set your booleans as you are now, but use the update method to determine if the “e” is pressed. If it detects the “e” AND your boolean is true for OnTriggerEnter, then process the transform.
what would that look like? im sorry im new at c# so i dont know what you mean by set your booleans as you are now or what the update method is. im also wondering why ontriggerstay didnt work, which i tried before seeing this comment but it only worked if the collider was on the door colliders line and not actually fully in the collider.
It would look similar to the following to capture your key presses. The Update Method runs every frame. it is an essential part or Unity Programming.
using UnityEngine;
public class KeyCodeExample : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Space key was pressed.");
}
if (Input.GetKeyUp(KeyCode.Space))
{
Debug.Log("Space key was released.");
}
}
}
I would advise you to begin with some unity learning to get you started with the basics of Unity and the Interface.
great thank you! ill look into unity learning generally i have been getting along well but i wouldnt say that i have learned much of the language or meanings of things so i will work on that. i also paired your code with on trigger stay which worked much better