ExploiterRoblox

Unity Walking System

Nov 3rd, 2020 (edited)
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Example : MonoBehaviour
  6. {
  7.     private CharacterController controller;
  8.     private Vector3 playerVelocity;
  9.     private bool groundedPlayer;
  10.     private float playerSpeed = 2.0f;
  11.     private float jumpHeight = 1.0f;
  12.     private float gravityValue = -9.81f;
  13.  
  14.     private void Start()
  15.     {
  16.         controller = gameObject.AddComponent<CharacterController>();
  17.     }
  18.  
  19.     void Update()
  20.     {
  21.         groundedPlayer = controller.isGrounded;
  22.         if (groundedPlayer && playerVelocity.y < 0)
  23.         {
  24.             playerVelocity.y = 0f;
  25.         }
  26.  
  27.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  28.         controller.Move(move * Time.deltaTime * playerSpeed);
  29.  
  30.         if (move != Vector3.zero)
  31.         {
  32.             gameObject.transform.forward = move;
  33.         }
  34.  
  35.         // Changes the height position of the player..
  36.         if (Input.GetButtonDown("Jump") && groundedPlayer)
  37.         {
  38.             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  39.         }
  40.  
  41.         playerVelocity.y += gravityValue * Time.deltaTime;
  42.         controller.Move(playerVelocity * Time.deltaTime);
  43.     }
  44. }
RAW Paste Data Copied