How can I make the RyacastHit2D hit an item from far, without my player standing beside it (while blocking my item by its own collider)?
I'm not really experienced with programming, which is why I don't understand how I can solve this problem. To interact with an item (or to click on it to show me a dialog) I have to stand beside the item. I would like to click on the item from further away. My second problem is, that I can't click on the item, because the collider from my player is blocking it. Does somebody know how I could solve those problems? I'm not really experienced with programming, which is why I don't understand how I can solve this problem. To interact with an item (or to click on it to show me a dialog) I have to stand beside the item. I would like to click on the item from further away. My second problem is, that I can't click on the item, because the collider from my player is blocking it. Does somebody know how I could solve those problems?
Here is how my script looks like:
if (Input.GetKeyDown(KeyCode.Mouse0) && dialogActive) { print("mouse was pressed");
mousePos = Input.mousePosition;
print("Screen Space" + mousePos);
mousePosWorld = mainCamera.ScreenToWorldPoint(mousePos);
print("World Space" + mousePosWorld);
mousePosWorld2D = new Vector2(mousePosWorld.x, mousePosWorld.y);
hit = Physics2D.Raycast(mousePosWorld2D, Vector2.zero);
if (hit.collider != null)
{
print("Collider recognized");
print("Name: " + hit.collider.gameObject.name);
if (hit.collider.gameObject.tag == "Items")
{
if (dialogBox.activeInHierarchy)
{
dialogBox.SetActive(false);
}
else
{
dialogBox.SetActive(true);
dialogText.text = dialog;
}
}
}
else
{
print("Collider not recognized");
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
dialogActive = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
dialogActive = false;
dialogBox.SetActive(false);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613012640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Best way to detect mouse inside radius of player? 0 Answers
RaycastAll from prefab is not picking up a collider from another prefab 1 Answer
reaching the limit of a Raycast[] array? 0 Answers
Compiler telling me raycast.collider is obsolete 1 Answer
Why doesn't my box collider work? (Unity2D) 2019.2.13.f1 0 Answers