Advertisement
S-Hend

SJH Task 2_Sem2_2024 Player Movement

Sep 12th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class P1PlayerMovement2D : MonoBehaviour
  6. {
  7.     public P1CharacterController2D controller;
  8.     public float runSpeed = 40f;
  9.  
  10.     float horizontalMove = 0f;
  11.     bool jump = false;
  12.     bool crouch = false;
  13.  
  14.     // Update is called once per frame 
  15.  
  16.     void Update()
  17.     {
  18.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
  19.  
  20.         if (Input.GetButtonDown("Jump"))
  21.         {
  22.             jump = true;
  23.         }
  24.  
  25.         if (Input.GetButtonDown("Crouch"))
  26.         {
  27.             crouch = true;
  28.         }
  29.         else if (Input.GetButtonUp("Crouch"))
  30.         {
  31.             crouch = false;
  32.         }
  33.  
  34.     }
  35.  
  36.     void FixedUpdate()
  37.     {
  38.         // Move our character  
  39.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
  40.         jump = false;
  41.  
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement