Advertisement
romi_fauzi

KartController.cs

Sep 3rd, 2021
1,619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class KartController : MonoBehaviour
  6. {
  7.     [SerializeField] float acceleration, gravity, steering;
  8.     [SerializeField] Rigidbody sphere;
  9.     [SerializeField] Animator spriteVisual;
  10.  
  11.     private float speed, currentSpeed;
  12.     private float rotate, currentRotation;
  13.  
  14.     // Update is called once per frame
  15.     void Update()
  16.     {
  17.         transform.position = sphere.transform.position;
  18.  
  19.         if (Input.GetButton("Fire1"))
  20.         {
  21.             speed = acceleration;
  22.         }
  23.  
  24.         if (Input.GetAxis("Horizontal") != 0f)
  25.         {
  26.             int dir = Input.GetAxis("Horizontal") > 0f ? 1 : -1;
  27.             float amount = Mathf.Abs(Input.GetAxis("Horizontal"));
  28.             Steer(dir, amount);
  29.         }
  30.  
  31.         spriteVisual.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
  32.  
  33.         currentSpeed = Mathf.SmoothStep(currentSpeed, speed, 12f * Time.deltaTime);
  34.         speed = 0f;
  35.         currentRotation = Mathf.Lerp(currentRotation, rotate, 4f * Time.deltaTime);
  36.         rotate = 0f;
  37.     }
  38.  
  39.     private void FixedUpdate()
  40.     {
  41.         sphere.AddForce(transform.forward * currentSpeed, ForceMode.Acceleration);
  42.  
  43.         sphere.AddForce(Vector3.down * gravity, ForceMode.Acceleration);
  44.  
  45.         transform.eulerAngles = Vector3.Lerp(transform.eulerAngles,
  46.             new Vector3(0f, transform.eulerAngles.y + currentRotation, 0f), 5f * Time.deltaTime);
  47.     }
  48.  
  49.     void Steer(int dir, float amount)
  50.     {
  51.         rotate = (steering * dir) * amount;
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement