Skip to main content
added class to show general structure
Source Link

To make your character constantly move you can apply a constant velocity or position update on each Update, the below is very crude but will get you started:

float movementSpeed = 5f;

void Update(){
    oPos = transform.position;
    transform.position = new Vector3(oPos.x + movementSpeed, oPos.y, oPos.z)
}

Altogether:

public class RunningCharacter : MonoBehaviour {
    
    float movementSpeed = 5f;

    bool runForwards = true;

    void Update(){
        UpdatePosition();
    }
    
    void UpdatePosition(){

        Vector3 oPos = transform.position;
        float calculatedPosition;

        if(runForwards){
            calculatedPosition = oPos.x + movementSpeed;
        } else {
            calculatedPosition = oPos.x - movementSpeed;
        }

        transform.position = new Vector3(oPos.x + movementSpeed, oPos.y, oPos.z)
    }

    void OnCollsionEnter(Collider collider){
        WallCollisionHandler(collider);
    }

    void WallCollsionHandler(Collider collider){
        if(collider.tag == "Left Facing Wall")
        {
            runForwards = false;

            // Or
            // JumpBackwards()
        }
        else if(collider.tag == "Right Facing Wall")
        {
            runForwards = true;

            // Or
            // JumpForwards()
        }
    }
}

To make your character constantly move you can apply a constant velocity or position update on each Update, the below is very crude but will get you started:

float movementSpeed = 5f;

void Update(){
    oPos = transform.position;
    transform.position = new Vector3(oPos.x + movementSpeed, oPos.y, oPos.z)
}

Altogether:

public class RunningCharacter : MonoBehaviour {
    
    float movementSpeed = 5f;

    bool runForwards = true;

    void Update(){
        UpdatePosition();
    }
    
    void UpdatePosition(){

        Vector3 oPos = transform.position;
        float calculatedPosition;

        if(runForwards){
            calculatedPosition = oPos.x + movementSpeed;
        } else {
            calculatedPosition = oPos.x - movementSpeed;
        }

        transform.position = new Vector3(oPos.x + movementSpeed, oPos.y, oPos.z)
    }

    void OnCollsionEnter(Collider collider){
        WallCollisionHandler(collider);
    }

    void WallCollsionHandler(Collider collider){
        if(collider.tag == "Left Facing Wall")
        {
            runForwards = false;

            // Or
            // JumpBackwards()
        }
        else if(collider.tag == "Right Facing Wall")
        {
            runForwards = true;

            // Or
            // JumpForwards()
        }
    }
}
added 9 characters in body
Source Link

Flip a switch depending on the walls' collision face:

bool runForwards = true;

public void WallCollsionHandler(Collider collider){
    if(collider.tag == "Left Facing Wall")
    {
        runForwards = false;

        // Or
        // JumpBackwards()
    }
    else if(collider.tag == "Right Facing Wall")
    {
        runForwards = true;

        // Or
        // JumpForwards()
    }
}

This will stop you getting stuck in a wall as well as switch your direction depending on the wall face you contacted. What you'll need to do is put 2 collision triggers on a wall to differentiate the direction you jump or run.

Walls

Flip a switch depending on the walls' collision face:

bool runForwards = true;

public void WallCollsionHandler(collider){
    if(collider.tag == "Left Facing Wall")
    {
        runForwards = false;

        // Or
        // JumpBackwards()
    }
    else if(collider.tag == "Right Facing Wall")
    {
        runForwards = true;

        // Or
        // JumpForwards()
    }
}

This will stop you getting stuck in a wall as well as switch your direction depending on the wall face you contacted. What you'll need to do is put 2 collision triggers on a wall to differentiate the direction you jump or run.

Walls

Flip a switch depending on the walls' collision face:

bool runForwards = true;

public void WallCollsionHandler(Collider collider){
    if(collider.tag == "Left Facing Wall")
    {
        runForwards = false;

        // Or
        // JumpBackwards()
    }
    else if(collider.tag == "Right Facing Wall")
    {
        runForwards = true;

        // Or
        // JumpForwards()
    }
}

This will stop you getting stuck in a wall as well as switch your direction depending on the wall face you contacted. What you'll need to do is put 2 collision triggers on a wall to differentiate the direction you jump or run.

Walls

Source Link

Flip a switch depending on the walls' collision face:

bool runForwards = true;

public void WallCollsionHandler(collider){
    if(collider.tag == "Left Facing Wall")
    {
        runForwards = false;

        // Or
        // JumpBackwards()
    }
    else if(collider.tag == "Right Facing Wall")
    {
        runForwards = true;

        // Or
        // JumpForwards()
    }
}

This will stop you getting stuck in a wall as well as switch your direction depending on the wall face you contacted. What you'll need to do is put 2 collision triggers on a wall to differentiate the direction you jump or run.

Walls