1
1
1mpulsee2021-03-12 12:08:35
Unity
1mpulsee, 2021-03-12 12:08:35

Auto control for android in unity how to do?

here is the script

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

public class carController : MonoBehaviour
{
    public GameObject car;
    public Transform centerOfMass;

    public WheelCollider[] frontCols;
    public Transform[] dataFront;
    public WheelCollider[] backCol;
    public Transform[] dataBack;
    public Transform[] FG;

    private float rpm = 0f;
    private float rpmAll = 0f;
    public float maxSpeed = 1000f;
    private float sideSpeed = 50;
    public float breakSpeed = 2000f;

    [SerializeField] Text speed;
    [SerializeField] Text RMP;
    public float CurrentSpeed
    {
        get { return car.GetComponent<Rigidbody>().velocity.magnitude * 1f; }
    }
    void Start()
    {
        GetComponent<Rigidbody>().centerOfMass = centerOfMass.localPosition;
    }

    void Update()
    {
        float vAxis = Input.GetAxis("Vertical");
        float hAxis = Input.GetAxis("Horizontal");
        bool breakeButton = Input.GetButton("Jump");
        //двигатель
        frontCols[0].motorTorque = vAxis * maxSpeed;
        frontCols[1].motorTorque = vAxis * maxSpeed;
        backCol[0].motorTorque = vAxis * maxSpeed;
        backCol[1].motorTorque = vAxis * maxSpeed;

        //тормоза
        if (breakeButton)
        {
            frontCols[0].brakeTorque = Mathf.Abs(frontCols[0].motorTorque) * breakSpeed;
            frontCols[1].brakeTorque = Mathf.Abs(frontCols[1].motorTorque) * breakSpeed;
            //backCol[0].brakeTorque = Mathf.Abs(frontCols[0].motorTorque) * breakSpeed;
            //backCol[1].brakeTorque = Mathf.Abs(frontCols[1].motorTorque) * breakSpeed;
        }
        else
        {
            frontCols[0].brakeTorque = 0;
            frontCols[1].brakeTorque = 0;
            backCol[0].brakeTorque = 0;
            backCol[1].brakeTorque = 0;
        }
        //руль
        frontCols[0].steerAngle = hAxis * sideSpeed;
        frontCols[1].steerAngle = hAxis * sideSpeed;
        //обновление граффики
        dataFront[0].Rotate(frontCols[0].rpm * Time.deltaTime, 0, 0);
        dataFront[1].Rotate(frontCols[1].rpm * Time.deltaTime, 0, 0);
        dataBack[0].Rotate(backCol[0].rpm * Time.deltaTime, 0, 0);
        dataBack[1].Rotate(backCol[1].rpm * Time.deltaTime, 0, 0);
        FG[0].localEulerAngles = new Vector3(dataFront[0].localEulerAngles.x * 0, hAxis * sideSpeed, dataFront[0].localEulerAngles.z * 0);
        FG[1].localEulerAngles = new Vector3(dataFront[1].localEulerAngles.x * 0, hAxis * sideSpeed, dataFront[1].localEulerAngles.z * 0);
        FG[2].localEulerAngles = new Vector3(19, 0, hAxis * -sideSpeed * 6);
        //спидометр
        rpmAll = frontCols[0].rpm + frontCols[1].rpm + backCol[0].rpm + backCol[1].rpm;
        rpm = rpmAll / 4 * -1 * 6;
        RMP.text = "RPM:" + Mathf.Abs((int)rpm).ToString();
        speed.text = "Speed:" + Mathf.RoundToInt(CurrentSpeed).ToString();
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question