My vehicle is not moving backwards with WheelCollider
Hi, I am new to Unity and have a problem with reversing the car. I am running an ML agent on car to move the car through a path evading few buildings. The car works fine when moving forward and steering. But when car hits a building, I want the car to reverse a bit so that ML agent have space to turn the steering and drive the car away. But the car is not moving in reverse direction, it is just stuck with the building.
Below is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class CarAgent : Agent {
public WheelCollider wheelFR;
public WheelCollider wheelFL;
public float maxMotorTorque = 150f;
public float currentSpeed;
public float maxSpeed = 200f;
public override void AgentAction(float[] vectorAction) {
float motorTorque = 0f;
float maxSteerAngle = 0f;
motorTorque = maxMotorTorque;
maxSteerAngle = vectorAction[1] * 30f;
PressAccelrate(motorTorque);
SteerCar(maxSteerAngle);
}
private void PressAccelrate(float motorTorque) {
currentSpeed = 2 * Mathf.PI * wheelFL.radius * wheelFL.rpm * 60 / 1000;
if (currentSpeed < maxSpeed) {
wheelFL.motorTorque = motorTorque;
wheelFR.motorTorque = motorTorque;
} else {
wheelFL.motorTorque = 0;
wheelFR.motorTorque = 0;
}
}
private void SteerCar(float steerAngle) {
wheelFL.steerAngle = steerAngle;
wheelFR.steerAngle = steerAngle;
}
public override float[] Heuristic() {
float motorTorque = 0f;
float steerAngle = 0f;
if (Input.GetKey(KeyCode.W)) {
motorTorque = maxMotorTorque;
}
if (Input.GetKey(KeyCode.A)) {
steerAngle = -0.5f;
}
if (Input.GetKey(KeyCode.D)) {
steerAngle = 0.5f;
}
if (Input.GetKey(KeyCode.S)) {
motorTorque = -maxMotorTorque;
}
return new float[] { motorTorque, steerAngle};
}
private void FixedUpdate() {
if (GetStepCount() % 5 == 0) {
RequestDecision();
} else {
RequestAction();
}
}
private void OnCollisionEnter(Collision collision) {
if (collision.transform.CompareTag("building")) {
CarReverse();
}
}
private void CarReverse() {
wheelFL.motorTorque = -maxMotorTorque;
wheelFR.motorTorque = -maxMotorTorque;
}
}
I can control the vehicle with keyboard buttons W, A, D but S is not reversing the car. Please help me on this. Let me know if I have to provide any more information. Also attaching an image of the scene. The red block is car, blue blocks are building, black strips are roads and rest is ground.
Your answer
Follow this Question
Related Questions
Tron-like movement and retractable / reversible trail? 0 Answers
Tank controlling with wheel colliders?,Tank control on wheels? 0 Answers
How to get car to reverse using motorTorque/WheelColliders? 0 Answers
How to make a loose wheels?,How to create a loose wheels? 0 Answers
One of meshes keeps following the camera in scene mode for some reason? 1 Answer