Advertisement
Guest User

PlayerMovement

a guest
May 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.  
  8.     public float KecBergerak = 0;
  9.     public float gravity = 6;
  10.     public float kecJalan = 4;
  11.     public float kecLari = 6;
  12.     public float kecLompat = 15;
  13.     private float v_velocity;
  14.     public float jump_Force = 10f;
  15.  
  16.     Vector3 arahGerak;
  17.  
  18.     CharacterController controller;
  19.  
  20.     void Start()
  21.     {
  22.         controller = GetComponent<CharacterController>();
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.         Move();
  28.     }
  29.  
  30.     void Move()
  31.     {
  32.         float gerakX = Input.GetAxis("Horizontal");
  33.         float gerakZ = Input.GetAxis("Vertical");
  34.  
  35.         arahGerak = new Vector3(gerakX, 0, gerakZ);
  36.  
  37.         if (controller.isGrounded)
  38.         {
  39.             //arahGerak = new Vector3(gerakX, 0, gerakZ);
  40.             if (Input.GetKey(KeyCode.LeftShift) && gerakZ == 1)
  41.             {
  42.                 KecBergerak = kecLari;
  43.             }
  44.             else
  45.             {
  46.                 KecBergerak = kecJalan;
  47.             }
  48.  
  49.             if (Input.GetKeyDown(KeyCode.Space))
  50.             {
  51.                 //arahGerak.y += kecLompat;
  52.                 v_velocity = jump_Force;
  53.             }
  54.  
  55.             if (Input.GetKey(KeyCode.LeftControl))
  56.             {
  57.                 controller.height = 0.75f;
  58.             }
  59.             else
  60.             {
  61.                 controller.height = 2f;
  62.             }
  63.  
  64.  
  65.  
  66.             arahGerak *= KecBergerak;
  67.         }
  68.  
  69.         v_velocity -= gravity * Time.deltaTime;
  70.  
  71.         arahGerak.y = v_velocity;
  72.         arahGerak = transform.TransformDirection(arahGerak);
  73.         controller.Move(arahGerak * Time.deltaTime);
  74.  
  75.        
  76.     }
  77.  
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement