Машинка ездит на таком скрипте
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class SimpleController : MonoBehaviour
{
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public float MaxTorque = 25;
public float steerAngle = 35;
void FixedUpdate ()
{
leftWheel.motorTorque = MaxTorque * Input.GetAxis("Vertical");
rightWheel.motorTorque = MaxTorque * Input.GetAxis("Vertical");
leftWheel.steerAngle = steerAngle * Input.GetAxis("Horizontal");
rightWheel.steerAngle = steerAngle * Input.GetAxis("Horizontal");
}
}
using System.Collections;
public class SimpleController : MonoBehaviour
{
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public float MaxTorque = 25;
public float steerAngle = 35;
void FixedUpdate ()
{
leftWheel.motorTorque = MaxTorque * Input.GetAxis("Vertical");
rightWheel.motorTorque = MaxTorque * Input.GetAxis("Vertical");
leftWheel.steerAngle = steerAngle * Input.GetAxis("Horizontal");
rightWheel.steerAngle = steerAngle * Input.GetAxis("Horizontal");
}
}
И еще добавлен скрипт antiroll bar чтобы ее не опрокидывало в поворотах
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class Antiroll : MonoBehaviour {
public WheelCollider WheelL;
public WheelCollider WheelR;
public float antiroll = 5000.0f;
public WheelHit hit;
void FixedUpdate () {
float travelL = 1.0f;
float travelR = 1.0f;
bool groundedL = WheelL.GetGroundHit(out hit);
if(groundedL)
travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
bool groundedR = WheelR.GetGroundHit(out hit);
if(groundedR)
travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius)/WheelR.suspensionDistance;
float antiRollForce = (travelL - travelR) * antiroll;
if(groundedL)
rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce , WheelL.transform.position);
if(groundedR)
rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);
}
}
using System.Collections;
public class Antiroll : MonoBehaviour {
public WheelCollider WheelL;
public WheelCollider WheelR;
public float antiroll = 5000.0f;
public WheelHit hit;
void FixedUpdate () {
float travelL = 1.0f;
float travelR = 1.0f;
bool groundedL = WheelL.GetGroundHit(out hit);
if(groundedL)
travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
bool groundedR = WheelR.GetGroundHit(out hit);
if(groundedR)
travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius)/WheelR.suspensionDistance;
float antiRollForce = (travelL - travelR) * antiroll;
if(groundedL)
rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce , WheelL.transform.position);
if(groundedR)
rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);
}
}
Stiffness factor на всех wheel collider установлен 0,82 для forward и 0,022 для sideways friction.
Вроде работает, не переворачивается, но машинку постоянно поворачивает в одну сторону.
Не могу никак разобраться почему так происходит.