Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I'm trying to implement a mechanic, but I can't figure out how to accommodate for the camera.

Discussion in 'Scripting' started by funnylittlecritter, Nov 26, 2021.

  1. funnylittlecritter

    funnylittlecritter

    Joined:
    Mar 17, 2021
    Posts:
    7
    Hey, everybody! I'm building a 3D platformer, and I'm struggling to implement a wall run mechanic. Here's how I've gotten it to work, presented through a handful of visual aides:


    Sounds pretty simple, right? I mean, at its core, it's just like any old character controller. Well... the problem is, controls are (seemingly) very inconsistent depending on the camera angle. For example, if the camera is looking dead-on at the surface you're running along, the controls are intuitive: pressing up on the stick makes your character go up, pressing left makes him go left, etc. However, if you were running along a wall and rotated it, say, 60 degrees on the Y axis, it's gonna look like pressing up on the stick should make your character move forward. I say this is seemingly inconsistent because it's all a trick of the camera. Even though you're still technically moving in the same direction, it doesn't feel right because of the camera angle.

    In short, trying to account for the surface directions AND camera angle while wall running seems nigh impossible.

    Got any suggestions on how I can solve this? Ideally, I'd like to develop a system where the direction the player moves in naturally changes depending on how foreshortening is impacting the wall he's running along.

    Thanks. -goob

    (Also, now that I'm writing this, this feels a bit more like a math problem than a true "scripting" issue... mods, if you feel like that too, feel free to blam this.)
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Your image doesn't load for me, so I think it's difficult for me to understand.
     
  3. funnylittlecritter

    funnylittlecritter

    Joined:
    Mar 17, 2021
    Posts:
    7
    Sorry! Just fixed that.
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    So, I'm not sure how the proper way is to do this, but here's my thought process on how I approached the subject.

    I first started by getting the Vector3 direction ray of the camera, and then flattening it onto the wall plane. This would give me the direction on the wall the camera is facing. You could use
    transform.forward
    on the current camera, with Vector3.ProjectOnPlane to achieve this easily without all the math.

    Now, a few problems I see immediately after doing this. Normally, the camera would be positioned at a slightly downwards angle, and would be kind of considered the "resting state". So it would definitely be weird if the camera were looking straight onto the wall, and pressing forward sent the character down. However, I believe any time the camera is looking upwards, I think we can safely assume that whatever direction they're facing, they would want to go forwards. (eg. if they're looking up, they want W to go up, and if they're looking left-up, they want W to go left-up).

    This creates a divide, so I'd think we would want to split up the controls into two parts, something like this.

    Code (CSharp):
    1. Vector3 directionVector; // the projected vector3 we got from camera.transform.forward
    2.  
    3. if (directionVector.y >= 0)
    4. {
    5.     // direction is fine, so we map controls as if directionVector is forward.
    6. }
    7. else
    8. {
    9.     // direction is not fine, as looking downwards is the natural state.
    10. }
    So really, the question is how do you want the controls to work while the camera is looking downwards at all? One could mirror what happens while the camera is upwards, this would make it so if you're looking in the direction of the wall, that pressing forwards would always move you up. I'm not sure how weird it would be while looking straight down or otherwise, but it would probably be the most natural way to do it.

    Edit: I hope this is sufficient enough as an explanation, I wanted to draw images for you but it was hard to figure out perspective and everything through a 2D image. I'll probably be off for tonight, but if you still need an explanation tomorrow I'll try to go more in depth of what I mean.