Restrict Y Plane Moving

Hi all! I’ve scripted a pretty simple third person script, however, in the cases that I might want my player to run up walls, I’d prefer not to move on the Y plane. How would I go about doing that from what I already have?

Control.js

#pragma strict
var moveSpeed                    : float                        = 18.0;                                            

private var controller            : CharacterController;      
private var isMove                : boolean                    = true;  
private var moveDirection         : Vector3                    = Vector3.zero;                    

function Awake(){
    controller = GetComponent(CharacterController);                                          
}
function Update() {  
    UpdateMoveDirection();  
    if(!controller.isGrounded){                                                                                 
        SetGravity();
    }
}
function UpdateMoveDirection(){                                                              
    var forward : Vector3 = Camera.main.transform.TransformDirection(Vector3.forward);      
    forward.y = 0;                                                                          
    forward = forward.normalized;                                                          
    var right : Vector3 = Vector3(forward.z, 0, -forward.x);                              
    var vertical : float = Input.GetAxis("Vertical");                                      
    var horizontal : float = Input.GetAxis("Horizontal");                              
  
    var targetDirection : Vector3 = horizontal * right + vertical * forward;              
    targetDirection *= moveSpeed;                                                          
    moveDirection.x = targetDirection.x;                                                  
    moveDirection.z = targetDirection.z;                                                   
  
    if(targetDirection != Vector3.zero){
        var rotation0 = transform.rotation;
        rotation0.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * Time.deltaTime / 2);
        transform.localRotation = rotation0;
    }
    if(isMove)                                                                              
        controller.Move(moveDirection * Time.deltaTime);
}
@script RequireComponent(Rigidbody)
@script RequireComponent(CharacterController)

Nevermind, I solved it. I just divided the targetDirection holding all the inputs and vectors with a really large number when I’m wall running. i.e
Control.js

if(isWall){
     moveDirection.x = targetDirection.x / 100;
     moveDirection.z = targetDirection.z / 100;
}

Whoa, so this wallrunning ready?

Does it rotate through sides of walls and upside down roofs?
i assume this will work through simple objects like box and cylinder?

No, that piece of script is not what makes the wall running happen, that just keeps the player from moving around while going up a wall. The function for the wall running is a bit more complex! :slight_smile:

If I wanted to run on roofs and sides of walls, then I’d have to rotate the player and disable the gravity as well.

Ah i see, so there’s a another function.

Why not use raycast for it? I noticed most solution for wallrunning projects raycasting…

I did. I have a finished wall running mechanic :slight_smile:

Can i see it?

While I don’t usually hand out my scripts, I’ll go ahead and give you the function as I may not be selling/finishing this project. It’s kind of my first time using Raycast, so it’s a good practice as well.

js.

function WallRun(){
    var hit : RaycastHit;
    var bump : boolean = false;
   
    if(Physics.Raycast(transform.position, transform.forward, hit, 1.8) && hit.collider.tag == "Wall"){
        isRotate = false;
        isWall = true;
        gravity = 0;
        transform.rotation = Quaternion.LookRotation(-hit.normal);
        moveDirection.y = wallRunSpeed;
    }
    else if(!(Physics.Raycast(transform.position, transform.forward, hit, 1.8) && hit.collider.tag == "Wall") && isWall){
        gravity = curGravity;
        isRotate = true;
        isWall = false;
        bump = true;
    }
    if(!isWall && bump)
        moveDirection.y = 20;
    bump = false;
    Debug.DrawRay(transform.position, transform.forward * hit.distance, Color.cyan);
}

Interesting…thank you for sharing this… i am trying to solve a wallrun equation myself…

When a wall creates something along 0- 90 degree on positive cartesian graph, the solution seems sensible to shoot a ray and then generate a normal based on it…
but when the angle produced is somewhat, less than 45 degree, a character may get stuck because of the height vs the angle of the normal…

You might want to configure your wall angles(works best for walls that are truly 90 degrees, anything else might be a bit more tricky). Definitely detect the walls using Raycast, and even use Vector3.Angle to get the angles. If you’re using a character controller, try using the controller’s slope limit to your advantage.

var controller : CharacterController;

if(controller.slopeLimit > Vector3.Angle(/* blah stuff here...*/)){
     //More code here
}