Script car controller sederhana untuk menjalankan mobil di Unity

Di postingan ini ingin saya share sebuah script car controller sederhana untuk menjalankan mobil.

Seperti apa penggunaannya bisa kalian lihat di video di bawahnya ya. Berikut scriptnya (Controller.cs):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
    //Adding audio source 
    //public AudioSource audioSourceIddle;
    //public float minimumPitchIdle = 0.5f;
    //public AudioSource audioSourceAcc;
    //public float minimumPitchAcc = 0.5f;




    //Making reference to wheel colliders
    [SerializeField] WheelCollider FrontRight;
    [SerializeField] WheelCollider FrontLeft;
    [SerializeField] WheelCollider BackRight;
    [SerializeField] WheelCollider BackLeft;

    //Getting Reference to phisycal wheel meshes
    [SerializeField] Transform FrontRightTransform;
    [SerializeField] Transform FrontLeftTransform;
    [SerializeField] Transform BackRightTransform;
    [SerializeField] Transform BackLeftTransform;


    public float acceleration = 30000f;
    public float BreakingForce = 8000f;
    public float MaxTurnAngle = 30f;


    private float CurrentAcceleration = 0f;
    private float CurrentBreakingForce = 0f;
    private float CurrentTurnAngle = 0f;
    private float CurrentAccSound = 0f;

    

    private void FixedUpdate()
    {
        //Making reference to the audio
        //audioSourceIddle = GetComponent<AudioSource>();
        //audioSourceAcc = GetComponent < AudioSource>();
        //audioSource.pitch = minimumPitch;

        //Get forward /backward acceleration from the vertical axis (W and S keys)
        CurrentAcceleration = acceleration * Input.GetAxis("Vertical")*(-1);
        CurrentAccSound = Input.GetAxis("Vertical");
        //setting up sound
        //if (CurrentAccSound < 0.0)
        //{
            //audioSourceIddle.pitch = (float)(minimumPitchIdle-0.1);
        //}
        //else if (CurrentAccSound > 0.7)
        //{
          //  audioSourceAcc.volume = minimumPitchAcc;
        //
        //else
        //{
         //   audioSourceIddle.pitch = minimumPitchIdle;
        //}
        
        

        //give CurrentBreakForce a value if space key is being pressed
        if (Input.GetKey(KeyCode.Space))
            CurrentBreakingForce = BreakingForce;
            //audioSource.pitch = minimumPitch;
        else
            CurrentBreakingForce = 0;
        //Front wheels acceleration
        FrontRight.motorTorque = CurrentAcceleration;
        FrontLeft.motorTorque = CurrentAcceleration;
        BackRight.motorTorque = CurrentAcceleration;
        BackLeft.motorTorque = CurrentAcceleration;
        //Apply braking to the four wheels
        FrontRight.brakeTorque = CurrentBreakingForce;
        FrontLeft.brakeTorque = CurrentBreakingForce;
        BackRight.brakeTorque = CurrentBreakingForce;
        BackLeft.brakeTorque = CurrentBreakingForce;

        //Stearing
        CurrentTurnAngle = MaxTurnAngle * Input.GetAxis("Horizontal");
        FrontRight.steerAngle = CurrentTurnAngle;
        FrontLeft.steerAngle = CurrentTurnAngle;

        //Update position and rotation of wheel meshes
        UpdateWheel(FrontRight, FrontRightTransform);
        UpdateWheel(FrontLeft, FrontLeftTransform);
        UpdateWheel(BackRight, BackRightTransform);
        UpdateWheel(BackLeft, BackLeftTransform);

    }

    void UpdateWheel(WheelCollider col, Transform trans) {
        //Get state of collider
        Vector3 position;
        Quaternion rotation;
        col.GetWorldPose(out position, out rotation);
        //Set wheel to the same state as collider
        trans.position = position;
        trans.rotation = rotation;
    }
}

Ini videonya:

loading...

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *