Home > Back-end >  Find player right/left side
Find player right/left side

Time:10-27

What I want to do is to find a way to know if there is something on the left/right of the player but for some reason, my code does not work.

    Vector3 rayDirection = new Vector3();

    switch (currentCameraSide)
    {
        case CameraSide.Left:
            rayDirection = -transform.right;

            break;
        case CameraSide.Right:
            rayDirection = transform.right;

            break;
    }

    // Start the ray pos from the player's head
    Vector3 playerStartPoint = transform.position   (transform.up * 1.5f);
    // The direction of the ray (left/right)
    Vector3 playerEndPoint = playerStartPoint   rayDirection;

    if (Physics.Raycast(playerStartPoint, playerEndPoint, out RaycastHit raycastHitA, sideCameraRayLenght, aimColliderMask))
    {
        Debug.Log($"Hitting: {raycastHitA.transform.name}");
    }

Basically what is happening is no matter what direction the player is facing, the direction of the hit is always left (possible world coords?)

CodePudding user response:

Raycast in the form you use it expects

  • a start position
  • a direction

you are giving it two positions ...

-> simply use the rayDirection itself as second parameter

  • Related