2d Wall Jump

I made a similar post in the Unity Answers section, but I haven’t got a reply yet on Wall Jumping so I’m posting this in the forums to see if anyone here can help me out.

First off, I want to say I’m very novice when it comes to coding. So I’m going to need some guidance here with this issue.

I watched the Live Training 16 video which covers 2D character controller. Here’s the Link. So I got the character moving, got double jump working. I haven’t been able to figure out how to do wall-jumping. I’ve been searching online for days, and I find tutorials that don’t work with 2D because they were made before the Unity 4.3 update, or I find explanations on how to do it, such as use ray-casting, but I have no clue how to write the code.

Here’s the code that I have, which is from that video I linked above. If someone can help me out with this, I would really appreciate it. Thanks in advance!

using UnityEngine;
using System.Collections;
 
public class CharacterControllerScript : MonoBehaviour {
 
    public float maxSpeed = 10f;
    bool facingRight = true;
 
    Animator anim;
 
    //sets up the grounded stuff
    bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    public float jumpForce = 700f;
 
    //double jump
    bool doubleJump = false;
 
    // Use this for initialization
    void Start () {
 
       anim = GetComponent<Animator>();
    }
 
    // Update is called once per frame
    void FixedUpdate () {
 
       // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
       grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
       anim.SetBool("Ground", grounded);
 
       if (grounded) {
         doubleJump = false;
       }
 
       anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
 
 
 
       float move = Input.GetAxis ("Horizontal");
 
       anim.SetFloat("Speed", Mathf.Abs (move));
 
       rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
 
       // If the input is moving the player right and the player is facing left...
       if(move > 0 !facingRight){
         // ... flip the player.
         Flip ();
       }// Otherwise if the input is moving the player left and the player is facing right...
       else if(move < 0  facingRight){
         // ... flip the player.
         Flip ();
       }
    }
    void Update(){
 
       // If the jump button is pressed and the player is grounded then the player should jump.
       if((grounded || !doubleJump)  Input.GetButtonDown("Jump")){
         anim.SetBool("Ground", false);
         rigidbody2D.AddForce(new Vector2(0, jumpForce));
 
         if(!doubleJump  !grounded){
          doubleJump = true;
         }
       }
 
    }
 
 
    void Flip(){
 
       // Switch the way the player is labelled as facing
       facingRight = !facingRight;
 
       //Multiply the player's x local cale by -1
       Vector3 theScale = transform.localScale;
       theScale.x *= -1;
       transform.localScale = theScale;
    }
}

Sounds like you’re going to require some sort of raycast here. The easiest way to accomplish this in my opinion is using a Physics.Raycast call. I’ll place some example code below.

// This code will tell 

RaycastHit hit;
if ( Physics.Raycast( transform.position, Vector.right, out hit) )
{
   float distanceToRightWall = hit.distance;
   if( distanceToRightWall <= playerWidth + 0.01f )
   {
      print( "i'm hitting the right hand wall!" );
      // Place logic for hitting right wall here
   }
}

This may work although I have not tested it. Also that 0.01f constant may require some tweaking, but hopefully you get the gist.

Hi, AnimalMother! Where exactly do I place that code? I put it inside Update and FixedUpdate and I get errors in both of those.

I see my mistake. From the errors I just fixed however my guess is you are relatively new to Unity. My advice is then to check out the scripting reference for the Physics class and specifically the static Raycast function and read through their mini examples there.

https://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

Here is my code in full, maybe it will help you to understand the usage of the physics class a little better. Attach the following script to a game object and then move it close to another object (on it’s positive X axis I believe). The console should light up and say there is something next to your object. Also, make sure you replace that 5.0f from the playerWidth variable with your player’s actual width (or the distance at which you would like to check for wall collisions). To check for collisions on the left, just use Vector3.left.

using UnityEngine;
using System.Collections;

public class PhysicsScript : MonoBehaviour {

	private float playerWidth = 5.0f;

	// Update is called once per frame
	void Update()
	{
		RaycastHit hit;
		if ( Physics.Raycast( transform.position, Vector3.right, out hit) )
		{
			
			float distanceToRightWall = hit.distance;
			if( distanceToRightWall <= playerWidth + 0.01f )
			{
				print( "i'm hitting the right hand wall!" );
				// Place logic for hitting right wall here
			}
			
		}
	}
}