[Solved] Instantiate Object on Raycast2D hit and rotate Instance

I want to move an instance of a gameObject along the outline of another gameobject. Its a 2D Project in Unity.
My current Code:

Vector3 mousePosition = m_camera.ScreenToWorldPoint(Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast(new Vector2(mousePosition.x, mousePosition.y), new Vector2(player.transform.position.x, player.transform.position.y));
    if (hit.collider != null &&  hit.collider.gameObject.tag == "Player") {
        if (!pointerInstance) {
            pointerInstance = Instantiate(ghostPointer, new Vector3(hit.point.x, hit.point.y, -1.1f), Quaternion.identity);
        } else if(pointerInstance) {
            pointerInstance.gameObject.transform.position = new Vector3(hit.point.x, hit.point.y, -1.1f);
            pointerInstance.gameObject.transform.eulerAngles = new Vector3(0f, 0f, hit.normal.x);
        }

    }

Unfortunately, the gameObject doesn’t rotate towards the mouse and the position on the left side of the playerObject is also sometimes off. I tried to use Instantiate() with Quaternion.LookRotation(hit.normal), but no luck either.

Here a rough sketch of what I want to achieve:

Any help is appreciated. Thanks!

Have you tried looking at the documentation for Physics2D.Raycast ? That shows you that the second argument is a direction, not a world position which I presume is your problem.

If you want to perform a cast between two points (line segment) then use Physics2D.Linecast.

If your player is just a circle of a specific radius then you do not need to use physics queries, just some simple math. From your diagram, calculate a normalized direction from the player position (2D) to the mouse position (2D) and scale that by the player radius. Here’s some pseudo code:

Vector2 mousePos = <blah>;
Vector2 playerPos = <blah>;
float playerRadius = <blah>;
var hitPoint = playerPos + ((mousePos - playerPos).normalized * playerRadius);

Thank you! In the meantime, I found a workaround but this information is definitely helpful for future projects.

I also learned that it’s better to use Mathematical way instead of a physical way(Raycasting) because in raycasting you have to throw ray several time for checking hit point, it could make your game lag.

Raycasting is fast, more so if you use the non-allocating ones. You’re doing something wrong if they’re taking too much time.

Less code is always faster so yeah, if there’s a simple algorithm then use it.