Facebook
From Thundering Goose, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 161
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.  
  7.     [SerializeField] float speed;
  8.     [SerializeField] float jumpForce;
  9.     [SerializeField] float wallJumpForce;
  10.     [SerializeField] float gravity;
  11.     [SerializeField] float wallDetectDist;
  12.     [SerializeField] float wallFriction;
  13.     [SerializeField] float wallSlideSpeedMax;
  14.  
  15.  
  16.     CharacterController controller;
  17.     ImpactReceiver impactReceiver;
  18.     Vector3 velocity;
  19.  
  20.     float vSpeed = 0;
  21.     float hSpeed = 0;
  22.     float lastJump;
  23.  
  24.     float wallJumpForceTime = 1.5f;
  25.  
  26.     bool wallSliding = false;
  27.     bool canJump = true;
  28.     bool hasWallJumped = false;
  29.  
  30.     Rigidbody rb;
  31.     int layer_mask;
  32.  
  33.     void Start()
  34.     {
  35.         rb = GetComponent<Rigidbody>();
  36.         controller = GetComponent<CharacterController>();
  37.         layer_mask = LayerMask.GetMask("Wall");
  38.     }
  39.  
  40.     void Update()
  41.     {
  42.  
  43.         //Reset variables when player is on the ground
  44.         if (controller.isGrounded)
  45.         {
  46.             Debug.Log("Resetting variables");
  47.             vSpeed = 0;
  48.             hSpeed = 0;
  49.             hasWallJumped = false;
  50.             canJump = true;
  51.             wallJumpForce = 0.2f;
  52.         }
  53.  
  54.         //Calculate horizontal movement
  55.         velocity.x = Input.GetAxis("Horizontal") * speed;
  56.  
  57.  
  58.         /*if (velocity.x == 1 || velocity.x == -1)
  59.             hSpeed = 0;
  60.             */
  61.  
  62.         /*
  63.          *
  64.          *
  65.          * FIX WALL JUMP
  66.          *
  67.          *
  68.          *
  69.          */
  70.        
  71.         if (hSpeed != 0)
  72.         {
  73.            
  74.             wallJumpForce -= Time.deltaTime;
  75.             //Debug.Log("Lerping hspeed [" + hSpeed + "]: jump force: " + wallJumpForce + " - 0 (" + wallJumpForce + ")");
  76.             //hSpeed = Mathf.Lerp(wallJumpForce, 0, );
  77.         }
  78.  
  79.         //Check if player can wall jump
  80.         WallJumping();
  81.  
  82.  
  83.         //Jumping
  84.         if (canJump && Input.GetButton("Jump"))
  85.         {
  86.  
  87.             if (!controller.isGrounded)
  88.             {
  89.                 Debug.Log("Jumping off wall - wall normal: " + GetWallNormal());
  90.                 hSpeed = wallJumpForce * GetWallNormal().x;
  91.             }
  92.             lastJump = Time.time;
  93.             vSpeed = jumpForce;
  94.             canJump = false;
  95.             Debug.Log("Jump");
  96.         }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.         //Calculate wall friction
  103.         WallFriction();
  104.  
  105.         //Calculate gravity
  106.         vSpeed += gravity * Time.deltaTime;
  107.  
  108.         //Apply vertical movement
  109.         velocity.y = vSpeed;
  110.         velocity.x += hSpeed;
  111.  
  112.         //Move character based on calculated movement above
  113.         controller.Move(velocity);
  114.  
  115.  
  116.     }
  117.  
  118.     void WallFriction()
  119.     {
  120.  
  121.  
  122.         wallSliding = false;
  123.  
  124.         //Check if player is touching wall, if its in the air and if is dropping down
  125.         if (IsTouchingWall() && !controller.isGrounded && vSpeed < 0)
  126.         {
  127.             wallSliding = true;
  128.  
  129.             if (vSpeed < -wallSlideSpeedMax)
  130.             {
  131.                 vSpeed = -wallSlideSpeedMax;
  132.             }
  133.         }
  134.     }
  135.  
  136.  
  137.     void WallJumping()
  138.     {
  139.         if (IsTouchingWall() && !controller.isGrounded && !hasWallJumped)
  140.         {
  141.             //Debug.Log("Player can jump again - touching: " + IsTouchingWall() + " , grounded: " + controller.isGrounded + " , wall jumped: " + hasWallJumped);
  142.             canJump = true;
  143.             hasWallJumped = true;
  144.         }
  145.     }
  146.  
  147.  
  148.     bool IsTouchingWall()
  149.     {
  150.  
  151.         RaycastHit hit;
  152.         if (Physics.Raycast(transform.position, Vector3.right, out hit, wallDetectDist, layer_mask) || Physics.Raycast(transform.position, Vector3.left, out hit, wallDetectDist, layer_mask))
  153.         {
  154.             //Debug.Log("Touched wall: " + hit.collider.gameObject.name);
  155.             return true;
  156.         }
  157.  
  158.         return false;
  159.        
  160.     }
  161.  
  162.  
  163.     Vector3 GetWallNormal()
  164.     {
  165.  
  166.         Vector3 wallNormal = Vector2.zero;
  167.  
  168.         RaycastHit hit;
  169.         if (Physics.Raycast(transform.position, Vector3.right, out hit, wallDetectDist, layer_mask) || Physics.Raycast(transform.position, Vector3.left, out hit, wallDetectDist, layer_mask))
  170.         {
  171.             wallNormal = hit.normal;
  172.         }
  173.  
  174.         return wallNormal;
  175.     }
  176.  
  177. }
  178.