Raycast detects walls, how to disable movement or input in direction of the wall?

Hi All,

I have a simple 2d movement script and a simple raycast script that will detect when the player touches a wall. I have no problem detecting the walls but I am not sure how to disable the player movement in the direction of the wall so that they can not walk through it.

This is a 2d setup similar to old jrpgs, like Final Fantasy for the NES, if that helps to envision the mechanics. Of course, I will need to be able to enable movement toward the wall again once the ray no longer detects the wall, i.e. after the player moves far enough in the opposite direction, but maybe I can figure that part out if someone can help me with disabling.

Movement script:

var speed: float = 15;
// Link to the animated sprite
private
var anim: tk2dSpriteAnimator;

// This script must be attached to the sprite to work.
anim = GetComponent(tk2dSpriteAnimator);


function FixedUpdate() {

    if (Input.GetButton("Horizontal")) {
        rigidbody.velocity = Vector3(speed, 0, 0);
        if (!anim.IsPlaying("right")) {
            anim.Play("right");
        }
        return;
    }

    if (Input.GetButton("-Horizontal")) {
        rigidbody.velocity = Vector3(-speed, 0, 0);
        if (!anim.IsPlaying("left")) {
            anim.Play("left");
        }
        return;
    }

    if (Input.GetButton("Vertical")) {
        rigidbody.velocity = Vector3(0, speed, 0);
        if (!anim.IsPlaying("up")) {
            anim.Play("up");
        }
        return;
    }

    if (Input.GetButton("-Vertical")  Time.time > 1) {
        rigidbody.velocity = Vector3(0, -speed, 0);
        if (!anim.IsPlaying("down")) {
            anim.Play("down");
        }
        return;
    }

    if (Input.GetButtonUp("-Vertical")) {
        if (!anim.IsPlaying("idle_down")) {
            anim.Play("idle_down");
        }
    }

    if (Input.GetButtonUp("Vertical")) {
        if (!anim.IsPlaying("idle_up")) {
            anim.Play("idle_up");
        }
    }

    if (Input.GetButtonUp("-Horizontal")) {
        if (!anim.IsPlaying("idle_left")) {
            anim.Play("idle_left");
        }
    }

    if (Input.GetButtonUp("Horizontal")) {
        if (!anim.IsPlaying("idle_right")) {
            anim.Play("idle_right");
        }
    }

}

Raycast script:

function FixedUpdate () {
 
   var up = transform.TransformDirection(Vector3.up);
   var right = transform.TransformDirection(Vector3.right);
   var hit : RaycastHit;

   Debug.DrawRay(transform.position, -up * 0.3, Color.green);
   Debug.DrawRay(transform.position, up * 0.9, Color.green);
   Debug.DrawRay(transform.position, -right * 0.9, Color.green);
   Debug.DrawRay(transform.position, right * 0.9, Color.green);
 
   if(Physics.Raycast(transform.position, -up, hit, 0.3)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.name == "BottomWall"){
           //Disable movement or input here
      }
   }

    if(Physics.Raycast(transform.position, up, hit, 0.9)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.name == "TopWall"){
           //Disable movement or input here
      }
   }

    if(Physics.Raycast(transform.position, -right, hit, 0.9)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.name == "LeftWall"){
           //Disable movement or input here
      }
   }

    if(Physics.Raycast(transform.position, right, hit, 0.9)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.name == "RightWall"){
           //Disable movement or input here
      }
   }
}

Thanks!

you could use booleans “canMoveLeft” “canMoveRight” etc. and include them in the conditions.

RaycastScript raycaster; // reference to raycast script

if (Input.GetButton("Horizontal")  raycaster.canMoveRight)
...

depending on what you are trying to do, if you really don’t need to check all directions every frame you could replace the raycast script’s update with functions which check the specific direction on demand. So instead of raycasting in all directions each frame you only call the specific raycast based on the player’s movement attempt. ie

movementScript

RaycastScript raycaster; // reference to raycast script

if (Input.GetButton("Horizontal")  raycaster.canMoveRight())
...

raycastScript

function canMoveRight() : boolean
{
... // insert raycast right code here
}

Yeah, that sounds like a good idea, I’ll give it a shot.