Can someone help me with my code , I’m new in game programming and I’m having this problem with my game when I do a wall jump my character go nuts
Here’s my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float movementSpeed;
public float jumpSpeed;
public float gravity;
bool isGrounded = false;
bool onWall = false;
bool jump = false;
bool wallJump = false;
void Start () {
}
void Update(){
if(Input.GetKey(KeyCode.D)){
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A)){
transform.Translate(Vector3.right * -movementSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.D)){
transform.Translate(Vector3.right * (movementSpeed*2) * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.A)){
transform.Translate(Vector3.right * (-movementSpeed*2) * Time.deltaTime);
}
if(Input.GetKey(KeyCode.Space) && isGrounded ){
jump = true;
}
if(Input.GetKey(KeyCode.Space) && onWall ){
wallJump = true;
}
}
void FixedUpdate(){
if(jump){
rigidbody2D.AddForce((Vector2.up * jumpSpeed));
isGrounded = false;
jump =false;
Debug.Log("Not grounded");
}
if(wallJump){
rigidbody2D.AddForce((Vector2.right * 400));
onWall = false;
wallJump =false;
Debug.Log("Not onwall");
}
}
void OnCollisionEnter2D(Collision2D other){
if(other.gameObject.CompareTag("Ground")){
isGrounded = true;
Debug.Log("grounded");
}
if(other.gameObject.CompareTag("Wall")){
onWall = true;
Debug.Log("onwall");
}
}
}