Advertisement
jretchy

Untitled

May 15th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. [Header("Player Movement")]
  2.     public CharacterController controller;
  3.     public float speed = 12f;
  4.  
  5.     public NavMeshAgent agent;
  6.  
  7.     [Header("Jumping")]
  8.     public float gravity = -9.81f;
  9.     public float jumpHeight = 3f;
  10.     public Transform groundCheck;
  11.     public float groundDistance = 0.4f;
  12.     public LayerMask groundMask;
  13.  
  14.     Vector3 velocity;
  15.     public bool isGrounded;
  16.  
  17.     [Header("Wall Running")]
  18.  
  19.     public bool isWallRunning;
  20.     public Transform wallCheck;
  21.     public float wallDistance = 0.02f;
  22.     public float runningSpeed = 1f;
  23.     public LayerMask runnableWall;
  24.     Vector3 runningDirection;
  25.  
  26.     float currentRunTIme = maxRunTime;
  27.     public const float maxRunTime = 1f;
  28.     float runDistance = 7f;
  29.     float runStoppingSpeed = 0.1f;
  30.  
  31.     private void Update()
  32.     {
  33.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  34.  
  35.         if (isGrounded && velocity.y < 0)
  36.         {
  37.             velocity.y = -2f;
  38.         }
  39.  
  40.         float x = Input.GetAxis("Horizontal");
  41.         float z = Input.GetAxis("Vertical");
  42.  
  43.         Vector3 move = transform.right * x + transform.forward * z;
  44.  
  45.         controller.Move(move * speed * Time.deltaTime);
  46.  
  47.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  48.         {
  49.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  50.             FindObjectOfType<SoundController>().jumpingSound.Play();
  51.         }
  52.  
  53.         velocity.y += gravity * Time.deltaTime;
  54.  
  55.         controller.Move(velocity * Time.deltaTime);
  56.  
  57.         if (isWallRunning == false)
  58.         {
  59.             gravity = -9.81f;
  60.         }
  61.         else if (isWallRunning == true)
  62.         {
  63.             gravity = 0f;
  64.         }
  65.  
  66.         if (controller.isGrounded)
  67.         {
  68.             isWallRunning = false;
  69.         }
  70.  
  71.  
  72.         if (Physics.CheckSphere(wallCheck.position, wallDistance, runnableWall) == true && controller.isGrounded == false)
  73.         {
  74.  
  75.             currentRunTIme = 0;
  76.             StartCoroutine(StopGravity());
  77.             FindObjectOfType<SoundController>().wallRunSound.Play();
  78.         }
  79.         if (currentRunTIme < maxRunTime)
  80.         {
  81.             runningDirection = transform.forward * runDistance;
  82.             currentRunTIme += runStoppingSpeed;
  83.         }
  84.         else
  85.         {
  86.             runningDirection = Vector3.zero;
  87.         }
  88.         controller.Move(runningDirection * Time.deltaTime * runningSpeed);
  89.  
  90.  
  91.     }
  92.  
  93.  
  94.     IEnumerator StopGravity()
  95.     {
  96.         isWallRunning = true;
  97.         yield return new WaitForSeconds(1);
  98.         isWallRunning = false;
  99.  
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement