Hello Guys
so I’m trying to move my car with motorTorque on wheel colliders
and when my car hit the side roads the speed drops to zero and the car wont move
what I’m trying to do is that i the speed decrease but dont become zero which the car stops moving
this is my carController Script
using UnityEngine;
using System.Collections.Generic;
using Cinemachine;
/// <summary>
/// Car Controls
/// </summary>
public class CarController : MonoBehaviour
{
/// <summary>
/// List of the wheel settings of the car.
/// </summary>
public List<WheelAxle> wheelAxleList;
/// <summary>
/// Car settings of the car.
/// </summary>
public CarSettings carSettings;
/// <summary>
/// Rigidbody of the car.
/// </summary>
private Rigidbody rbody;
/// <summary>
/// Calculated speed of the car.
/// </summary>
public float speed = 0;
private bool isJumping = false;
private bool canVibrate;
public bool IsJumping { get => isJumping; set => isJumping = value; }
private Transform playerTransform;
private CinemachineImpulseSource impulseSource;
[SerializeField] private TrailRenderer[] tyreMarks;
private void Start() {
playerTransform = transform;
///create rigidbody
rbody = GetComponent<Rigidbody>();
///set mass of the car
rbody.mass = carSettings.mass;
///set drag of the car
rbody.drag = carSettings.drag;
//set the center of mass of the car
rbody.centerOfMass = carSettings.centerOfMass;
impulseSource = GetComponent<CinemachineImpulseSource>();
}
/// <summary>
/// Visual Transformation of the car wheels.
/// </summary>
/// <param name="wheelCollider"></param>
/// <param name="wheelMesh"></param>
public void ApplyWheelVisuals(WheelCollider wheelCollider, GameObject wheelMesh) {
///get position and rotation of the WheelCollider
wheelCollider.GetWorldPose(out Vector3 position, out Quaternion rotation);
///calculate real rotation of the wheels
Quaternion realRotation = rotation * Quaternion.Inverse(wheelCollider.transform.parent.rotation)
* playerTransform.rotation;
wheelMesh.transform.SetPositionAndRotation(position, realRotation);
}
public void FixedUpdate() {
///get speed of the car
speed = rbody.velocity.magnitude;
if (playerTransform.position.y > 4) {
rbody.freezeRotation = false;
canVibrate = true;
if (playerTransform.position.y > 8){
IsJumping = true;
rbody.freezeRotation = true;
}
} else {
IsJumping = false;
if (playerTransform.position.y < 2f) {
if (canVibrate) {
canVibrate = false;
CameraShakeManager.instance.CameraShake(impulseSource);
Handheld.Vibrate();
}
rbody.freezeRotation = true;
playerTransform.rotation = Quaternion.Euler(0, 0, 0);
}
}
float motor;
float handBrake;
if (Input.touchCount > 0 || Input.GetKeyDown(KeyCode.LeftShift)) {
motor = carSettings.motorTorque / 2f;
if(Input.touches[0].position.x > 960){
motor = carSettings.motorTorque * 1.5f;
// rbody.AddForce(carSettings.motorTorque * Time.deltaTime * 90 * rbody.transform.forward);
}
if(Input.touches[0].position.x < 960){
StartTyreMarks();
handBrake = carSettings.motorTorque * 90;
} else {
handBrake = 0;
}
} else {
// rbody.AddForce(carSettings.motorTorque * Time.deltaTime * 90 * rbody.transform.forward);
motor = carSettings.motorTorque / 2f;
StopTyreMarks();
handBrake = 0;
}
if (!isJumping) {
float move = Input.acceleration.x;
if (move > 0.05f) {
rbody.AddForce(70000 * carSettings.steeringAngle * Time.deltaTime * rbody.transform.right);
} else {
if (move < -0.05f) {
rbody.AddForce(70000 * carSettings.steeringAngle * Time.deltaTime * -rbody.transform.right);
}
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) {
rbody.AddForce(200000 * carSettings.steeringAngle * Time.deltaTime * rbody.transform.right);
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
rbody.AddForce(200000 * carSettings.steeringAngle * Time.deltaTime * -rbody.transform.right);
}
}
///calculate motor break
// float handBrake = Input.GetKey(KeyCode.Space) == true ? carSettings.motorTorque * 1 : 0;
///iterate all wheel axles
foreach (WheelAxle wheelAxle in wheelAxleList)
{
///this is a steering axle
// if (wheelAxle.steering) {
///apply steering
// wheelAxle.wheelColliderLeft.steerAngle = steering;
// wheelAxle.wheelColliderRight.steerAngle = steering;
// }
///this is motor axle
if (wheelAxle.motor) {
///apply motor torque
wheelAxle.wheelColliderLeft.motorTorque = motor;
wheelAxle.wheelColliderRight.motorTorque = motor;
}
///apply motor break
wheelAxle.wheelColliderLeft.brakeTorque = handBrake;
wheelAxle.wheelColliderRight.brakeTorque = handBrake;
///apply wheel visuals
ApplyWheelVisuals(wheelAxle.wheelColliderLeft, wheelAxle.wheelMeshLeft);
ApplyWheelVisuals(wheelAxle.wheelColliderRight, wheelAxle.wheelMeshRight);
}
}
private void StopTyreMarks() {
foreach (TrailRenderer item in tyreMarks){
item.emitting = false;
}
}
private void StartTyreMarks() {
foreach (TrailRenderer item in tyreMarks){
item.emitting = true;
}
}
}
as you can see i even tried the addForce but the result was the same
i tried to change the force mode but the result was not good
this is the collision script :
private void OnCollisionStay(Collision other) {
if (other.collider.gameObject.CompareTag("Side Road")) {
rbody.velocity = new(0,0,playerSpeed);
Handheld.Vibrate();
foreach (ContactPoint contact in other.contacts) {
ParticleSystem effect = ObjectPoolManger.SpawnParticle(hittingSideRoad, contact.point, Quaternion.identity);
effect.transform.SetParent(GameObject.Find("Hit Effect").transform);
StartCoroutine(ReturnParticleToPool(effect));
}
}
if (other.collider.gameObject.CompareTag("Container")) {
timer += Time.deltaTime;
if (timer > timeToStayOnTopOfJumpingTruck) {
GameOver();
}
}
}
what i tried in collision was that i would store the velocity when player hit the side road and make the player move with that velocity but as i spawn roads (its an endless game) the new road has new sideroads and the players enter new collision which then changed the velocity and eventually the velocity drops to zero
