zimethsuki

Move Controller

Jun 24th, 2021 (edited)
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1.     public CharacterController controller;
  2.     public Transform cam;
  3.  
  4.     public float speed;
  5.  
  6.     public float turnSmoothTime = 0.1f;
  7.  
  8. //------------EDIT---------------
  9.  
  10. //set Gravitasi nya
  11.     private float gravity = 9.8f;
  12.     private float verticalVelocity;
  13.  
  14.     private Vector3 moveDir = Vector3.zero;
  15.  
  16. //---------------------------
  17.  
  18.     float turnSmoothVelocity;
  19.  
  20.  
  21.  
  22.     void Update()
  23.     {
  24.         float horiz = Input.GetAxisRaw("Horizontal");
  25.         float vertic = Input.GetAxisRaw("Vertical");
  26.         Vector3 direction = new Vector3(horiz, 0f, vertic).normalized;
  27.  
  28.         //------------EDIT---------------
  29.        
  30.         // cek grounded gak?
  31.         if (characterController.isGrounded )
  32.         {
  33.             //kalo iya jangan di tarik
  34.             verticalVelocity = 0f;
  35.         }
  36.         else
  37.         {
  38.             //klo nga, ditarik gravitasi
  39.             verticalVelocity -= gravity * Time.deltaTime;
  40.         }
  41.  
  42.         //---------------------------
  43.  
  44.         if(direction.magnitude >= 0.1f)
  45.         {
  46.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
  47.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
  48.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
  49.  
  50.             moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
  51.            
  52.         }
  53.  
  54.         //---------EDIT------------------
  55.         else
  56.         {
  57.             moveDir = Vector3.zero;
  58.         }
  59.            
  60.         //masukin gravitasinya
  61.         moveDir.y = verticalVelocity;
  62.         controller.Move(moveDir.normalized * speed * Time.deltaTime);
  63.         //---------------------------
  64.            
  65.     }
Add Comment
Please, Sign In to add comment