Search Unity

Help with Learning 3D Raycasting

Discussion in 'Physics' started by QuindecimX, Oct 20, 2021.

  1. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    Does anyone know of any current raycast tutorials for Unity 3D? I've been trying to learn how to utilize raycasting in 3d to make my game character be able to both recognize and push himself up on a wall/cover. However, any forums I found don't reach a solution to help the person, and videos I find are overtly convoluted, and don't solve my problem.

    There just doesn't seem to be many sources out there that at least teach the basics of using raycasts. Does Unity already have a package that does this automatically or something? Or is there a popular program in the Asset Store that sets up raycasting for you (like how Cinemachine once was before in was included in Unity for free)?

    Please let me know. Thanks.
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Something like this to start with? Assuming climbable walls are tagged with "wall", and the player has the method PlayerHasRecognizedAwall_TimeToPushAndClimb(collider theWall);

    Doing the raycasting in Player Update loop, or FixedUpdate:
    Code (CSharp):
    1. if (Physics.Raycast(transform.position, transform.forward, out objectHit, 1f))
    2. {
    3.     if (objectHit.collider.tag == "wall") {
    4.        PlayerHasRecognizedAwall_TimeToPushAndClimb(objectHit.collider);
    5.    }
    6. }
     
    Last edited: Oct 21, 2021
  3. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    Thanks but what I'm trying to do is make my character push up on a wall like a cover system. Unlike most modern games that uses a button to toggle being in cover, I'm implementing a control scheme where you simply push up on the wall to get into cover mode, and stop pushing on the wall to go back to normal movement. (If you've ever played any Metal Gear Solid games from 1 - 3 you'll know what I'm talking about.)

    I've played around in Unity's Raycast documentation. So I'm able to make the character recognize when they're near cover. The problem now is getting the character to turn around and push their back on the wall. I've already created all these animations for having their back on a wall, as well as moving left and right while on the wall.

    Not only do they need to correctly orient themselves to the wall with their back to it, but the character needs to be able to sidle left and right along the wall. These are my two biggest problems I've spent weeks trying to solve.