using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [SerializeField] float speed; [SerializeField] float jumpForce; [SerializeField] float wallJumpForce; [SerializeField] float gravity; [SerializeField] float wallDetectDist; [SerializeField] float wallFriction; [SerializeField] float wallSlideSpeedMax; CharacterController controller; ImpactReceiver impactReceiver; Vector3 velocity; float vSpeed = 0; float hSpeed = 0; float lastJump; float wallJumpForceTime = 1.5f; bool wallSliding = false; bool canJump = true; bool hasWallJumped = false; Rigidbody rb; int layer_mask; void Start() { rb = GetComponent(); controller = GetComponent(); layer_mask = LayerMask.GetMask("Wall"); } void Update() { //Reset variables when player is on the ground if (controller.isGrounded) { Debug.Log("Resetting variables"); vSpeed = 0; hSpeed = 0; hasWallJumped = false; canJump = true; wallJumpForce = 0.2f; } //Calculate horizontal movement velocity.x = Input.GetAxis("Horizontal") * speed; /*if (velocity.x == 1 || velocity.x == -1) hSpeed = 0; */ /* * * * FIX WALL JUMP * * * */ if (hSpeed != 0) { wallJumpForce -= Time.deltaTime; //Debug.Log("Lerping hspeed [" + hSpeed + "]: jump force: " + wallJumpForce + " - 0 (" + wallJumpForce + ")"); //hSpeed = Mathf.Lerp(wallJumpForce, 0, ); } //Check if player can wall jump WallJumping(); //Jumping if (canJump && Input.GetButton("Jump")) { if (!controller.isGrounded) { Debug.Log("Jumping off wall - wall normal: " + GetWallNormal()); hSpeed = wallJumpForce * GetWallNormal().x; } lastJump = Time.time; vSpeed = jumpForce; canJump = false; Debug.Log("Jump"); } //Calculate wall friction WallFriction(); //Calculate gravity vSpeed += gravity * Time.deltaTime; //Apply vertical movement velocity.y = vSpeed; velocity.x += hSpeed; //Move character based on calculated movement above controller.Move(velocity); } void WallFriction() { wallSliding = false; //Check if player is touching wall, if its in the air and if is dropping down if (IsTouchingWall() && !controller.isGrounded && vSpeed < 0) { wallSliding = true; if (vSpeed < -wallSlideSpeedMax) { vSpeed = -wallSlideSpeedMax; } } } void WallJumping() { if (IsTouchingWall() && !controller.isGrounded && !hasWallJumped) { //Debug.Log("Player can jump again - touching: " + IsTouchingWall() + " , grounded: " + controller.isGrounded + " , wall jumped: " + hasWallJumped); canJump = true; hasWallJumped = true; } } bool IsTouchingWall() { RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.right, out hit, wallDetectDist, layer_mask) || Physics.Raycast(transform.position, Vector3.left, out hit, wallDetectDist, layer_mask)) { //Debug.Log("Touched wall: " + hit.collider.gameObject.name); return true; } return false; } Vector3 GetWallNormal() { Vector3 wallNormal = Vector2.zero; RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.right, out hit, wallDetectDist, layer_mask) || Physics.Raycast(transform.position, Vector3.left, out hit, wallDetectDist, layer_mask)) { wallNormal = hit.normal; } return wallNormal; } }