N
N
Nikita Salnikov2020-04-16 00:40:37
C++ / C#
Nikita Salnikov, 2020-04-16 00:40:37

Why is the script not working correctly?

hello. please tell me why the script is not working correctly

using UnityEngine;
using System.Collections;

public class CarController : MonoBehaviour {
  
  public WheelCollider[] WColForward;
  public WheelCollider[] WColBack;
  
  public float maxSteer = 30; //1
  public float maxAccel = 25; //2
  public float maxBrake = 50; //3
  
  
  // Use this for initialization
  void Start () {
  
  }
  
  
  void FixedUpdate () {
    
    float accel = 0;
    float steer = 0;
        
    accel = Input.GetAxis("Vertical");  //4
    steer = Input.GetAxis("Horizontal");	 //4	
    
    CarMove(accel,steer); //5
    
  }
  
  private void CarMove(float accel,float steer){ //5
    
    foreach(WheelCollider col in WColForward){ //6
      col.steerAngle = steer*maxSteer; //6
    }
    
    if(accel == 0){ //7
      foreach(WheelCollider col in WColBack){  //7
        col.brakeTorque = maxBrake; //7
      }	
      
    }else{ //8
                
      foreach(WheelCollider col in WColBack){ //8
        col.brakeTorque = 0; //8
        col.motorTorque = accel*maxAccel; //8
      }	
    }
  }
}

it seems to work, but it seems that the car, as if pulled by the ears, barely accelerates and barely turns. release the button stops abruptly. if I start to change the indicators, then it generally behaves inadequately, that is, for example, it starts to slow down when turning.
lesson taken from here: https://habr.com/en/post/115557/

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