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;
}
}