Search code examples
c#unity-game-enginedirectionraycasting

Unity3D raycasting wrong direction c#


foreach (Transform transf in spawnPoint) {
        if (Physics.Raycast (transf.position, player.position, out test, 1000))
            Debug.DrawLine (transf.position, test.point, Color.yellow);
    }

This is my code. And that is what I get: enter image description here

Player is a gameobject with camera, transforms are not visible here, but they are on the left of every yellow line. Why isn't it pointing player? What to do you ray pointing player? Greenline works similar to yellows, but it's point canvas for no reason.


Solution

  • Second parameter of Physycs.Raycast is direction so you should compute direction from transforms to player:

    foreach (Transform transf in spawnPoint) {
        if (Physics.Raycast (transf.position, player.position - transf.position, out test, 1000))
            Debug.DrawLine (transf.position, test.point, Color.yellow);
    }