Okay let me start of with the normal:
Im new to Unity,
I have only been coding in C# for about a week,
I apologize if my code looks messy.
I am having the right bollock ache with getting my little 2D guy jumping off of a wall.
the Y axis is fine… the player jumps according to what number I decide should be the force for jumping up when against a wall
but…
for the life of me i can not get him to jumpBackwards away from the wall
here is my code…
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// moving
public float MaxSpeed = 10f;
private bool faceRight = true;
//Jumping
public float airControl = 0.5f;
public float JumpForce = 300f;
public float doubleJumpForce = 2f;
private float maxJumpTime = 0.1f;
public int jumpNumber;
public int totalJump = 2;
private float timer;
private bool canJump;
// What is Wall / Wall Jump
public bool TouchingWall = false;
public Transform WallCheck;
public LayerMask whatIsWall;
private float WallRadius = 1f;
public int PushForceRight;
public int PushForceLeft;
public float WallJumpForce;
// What is Ground
private float groundRadius = 0.2f;
public bool Grounded = false;
public Transform groundCheck;
public LayerMask whatIsGround;
public bool touchingWallLeft = false;
public bool touchingWallRight = false;
// crouch
private bool crouch = false;
// Spawning
public Transform target;
// Animator
Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void FixedUpdate () {
Grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", Grounded);
anim.SetBool ("Crouch", crouch);
// Wall Jump
TouchingWall = Physics2D.OverlapCircle (WallCheck.position, WallRadius, whatIsWall);
// moving
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
// When On The Ground
if (Grounded) {
canJump = true;
jumpNumber = totalJump;
touchingWallLeft = false;
touchingWallRight = false;
}
if (Grounded) { // movement on the floor
GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * MaxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
} else { // movement in the air
GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * MaxSpeed * airControl, GetComponent<Rigidbody2D> ().velocity.y);
}
if (move > 0 && !faceRight) // flips the direction of character
Flip ();
else if (move < 0 && faceRight)
Flip ();
}
void Update(){
if (Grounded && Input.GetKeyDown (KeyCode.Space)) { // Jump
timer = 0;
canJump = true;
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, JumpForce));
} else if (Input.GetKey (KeyCode.Space) && canJump && timer < maxJumpTime) { // Jump Higher when pressed longer
timer += Time.deltaTime;
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, JumpForce));
} else if (Input.GetKeyDown (KeyCode.Space) && !Grounded && jumpNumber>0) { // Double Jump
jumpNumber--;
timer = 0;
canJump = true;
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, doubleJumpForce);
}else {
}canJump = false;
// HERE IS WHERE MY ISSUE IS *****************************************************
if (TouchingWall && faceRight) {
touchingWallRight = true;
touchingWallLeft = false;
if (Input.GetKeyDown (KeyCode.Space) && touchingWallRight) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (PushForceRight * 1, WallJumpForce); // WallJumpForce works great but pushForceRight does nothing... even when i change the 1 to -1
}
}
if (TouchingWall && !faceRight) {
touchingWallLeft = true;
touchingWallRight = false;
if (Input.GetKeyDown (KeyCode.Space)&& touchingWallLeft) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (PushForceLeft * -1, WallJumpForce);
}
}
// *****************************************************
if (Input.GetKey ("down")) { // crouch when down pressed
crouch = true;
GetComponent<BoxCollider2D>().enabled = false; // disables box collider
} else{
crouch = false;
GetComponent<BoxCollider2D>().enabled = true;
}
if(Input.GetKeyDown (KeyCode.W)) {
this.transform.position = target.position; // respawn character
}
}
void OnTriggerEnter2D(Collider2D collider)
{
HitByLaser(collider);
}
void HitByLaser(Collider2D laserCollider)
{
if(laserCollider.gameObject.tag == "Enemy") { // when player hits Enemy respawn
this.transform.position = target.position;
}
}
void Flip(){ // flips direction of character
faceRight = !faceRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}