I have a Vehicle that runs on batteries and gets charged. I’m finding the “Charging” bool is always true. it won’t even let me uncheck it in the inspector.
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
public enum Axel
{
Front,
Rear
}
[Serializable]
public struct Wheel
{
public GameObject model;
public WheelCollider collider;
public Axel axel;
}
public class TrainController : MonoBehaviour
{
[SerializeField]
private float maxAcceleration = 20.0f;
[SerializeField]
private float turnSensitivity = 1.0f;
[SerializeField]
private float maxSteerAngle = 45.0f;
[SerializeField]
private Vector3 _centerOfMass;
[SerializeField]
private List<Wheel> wheels;
[SerializeField]
private bool forward;
private float inputX, inputY;
private Rigidbody _rb;
private UIManager _uiManager;
[SerializeField]
private float _battery = 100f;
private float batteryDecreaseRate = 5f;
private float _maxBattery = 100f;
[SerializeField]
private bool _powered;
private SolarPanel _solarPanel;
private float _chargeRate;
private float _batteryCharge;
[SerializeField]
private bool _charging;
private void Start()
{
_rb = GetComponent<Rigidbody>();
_rb.centerOfMass = _centerOfMass;
forward = false;
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
_solarPanel = GameObject.Find("Solar Panel").GetComponent<SolarPanel>();
if (_uiManager == null)
{
Debug.LogError("The UI Manager is Null.");
}
if (_solarPanel == null)
{
Debug.LogError("The Solar Panel is Null.");
}
}
private void Update()
{
//AnimateWheels();
GetInputs();
_chargeRate = _solarPanel.PowerProduct();
if (_battery <= _maxBattery)
{
_batteryCharge += _chargeRate * Time.deltaTime;
_charging = true;
}
else
{
_charging = false;
}
_uiManager.UpdateBattery(_battery);
if (_battery <= 0)
{
_powered = false;
}
else
{
_powered = true;
if (_charging == false)
{
_battery -= batteryDecreaseRate * Time.deltaTime;
}
}
}
private void LateUpdate()
{
if (_powered == true)
{
Move();
//Turn();
}
}
private void GetInputs()
{
inputX = Input.GetAxis("Horizontal");
inputY = Input.GetAxis("Vertical");
}
private void Move()
{
if (forward == true)
{
foreach (var wheel in wheels)
{
wheel.collider.motorTorque = maxAcceleration * 500 * Time.deltaTime;
}
}
}
//private void Turn()
//{
// if (forward == true) {
// foreach (var wheel in wheels)
// {
// if (wheel.axel == Axel.Front)
// {
// var _steerAngle = inputX * turnSensitivity * maxSteerAngle;
// wheel.collider.steerAngle = Mathf.Lerp(wheel.collider.steerAngle, _steerAngle, 0.5f);
// }
// }
// }
//}
//private void AnimateWheels()
//{
// foreach (var wheel in wheels)
// {
// Quaternion _rot;
// Vector3 _pos;
// wheel.collider.GetWorldPose(out _pos, out _rot);
// wheel.model.transform.position = _pos;
// wheel.model.transform.rotation = _rot;
// }
//}
}