Destroy object with KeyCode via RaycastHit
Hi! Long story short: Player moves to an item. Once within "maxCheckDistance" (3f), see the item, promptText appear from HUD if on the same layermask. Everything works fine for now. What I want to do: When player get close to object (within range) and hit "E" as a KeyDown(KeyCode), object spotted by RaycastHit has to be destroyed.
Collider, tag and layermask were added to parent object (the one I am doing the interaction with) - image provided.
Here is the code:
public class InteractionManager : MonoBehaviour
{
public float checkRate = 0.05f;
private float lastCheckTime;
public float maxCheckDistance = 3f;
public LayerMask layerMask;
private GameObject curInteractGameObject;
public IInteractable curInteractable;
public TextMeshProUGUI promptText;
private Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
//True every "checkrate" seconds
if (Time.time - lastCheckTime > checkRate)
{
lastCheckTime = Time.time;
//Create a ray from the center of the screen pointing in the direction the player is looking
Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit hit;
//Raycast hit something?
if (Physics.Raycast(ray, out hit, maxCheckDistance, layerMask))
{
//Is it interactable? If so, set as current interactable
if(hit.collider.gameObject != curInteractGameObject)
{
curInteractGameObject = hit.collider.gameObject;
curInteractable = hit.collider.GetComponent<IInteractable>();
SetPromptText();
Debug.Log("ray hit (tag): " + hit.collider.gameObject.tag);
//If "E" is pressed while interacting, destroy object found by Raycast
if (Input.GetKeyDown(KeyCode.E))
{
//print("E was pressed while looking at object");
//Debug.Log("E was pressed while looking at object");
Destroy(hit.collider.gameObject);
}
}
}
//If not interactable
else
{
curInteractGameObject = null;
curInteractable = null;
promptText.gameObject.SetActive(false);
}
}
}![alt text][1]
Thanks in advance! :)
[1]: /storage/temp/178580-image.jpg
Answer by Hellium · Apr 02, 2021 at 10:05 PM
void Update()
{
//True every "checkrate" seconds
if (Time.time - lastCheckTime > checkRate)
{
lastCheckTime = Time.time;
//Create a ray from the center of the screen pointing in the direction the player is looking
Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit hit;
//Raycast hit something?
if (Physics.Raycast(ray, out hit, maxCheckDistance, layerMask))
{
//Is it interactable? If so, set as current interactable
if(hit.collider.gameObject != curInteractGameObject)
{
curInteractGameObject = hit.collider.gameObject;
curInteractable = hit.collider.GetComponent<IInteractable>();
SetPromptText();
Debug.Log("ray hit (tag): " + hit.collider.gameObject.tag);
}
}
//If not interactable
else
{
curInteractGameObject = null;
curInteractable = null;
promptText.gameObject.SetActive(false);
}
}
if(curInteratableGameObject != null && Input.GetKeyDown(KeyCode.E))
{
Debug.Log("E was pressed while looking at object " + curInteratableGameObject.name, curInteratableGameObject);
Destroy(curInteratableGameObject);
}
}
Changed the "Interatable" for "Interact" and it worked! Is there a reason why it has to be out of Raycast check? I am kinda new in development and trying to learn it by myself. Thanks!
I literally copy-pasted your code and move one condition outside the raycast check because you perform the latter only every checkRate
seconds. Your player would need to be extremely precise to perform the Key down at the exact frame the raycast is performed.
Ohhh!!!! So it is because of the checkRate seconds. I was wondering why I was able to see it sometimes but not always. Thanks for the constructive feedback, really appreciated :)