Iteration 1, added Kenney art. Also added braking.

Next iteration will be check points and lap times UI.
Then need collision of some sort, and some sort of terrain effects
(grass slows).
This commit is contained in:
2021-02-15 20:30:42 -06:00
parent e51c1d3b6c
commit 3e3a18c11d
270 changed files with 38817 additions and 442 deletions
+70 -68
View File
@@ -1,84 +1,86 @@
using System;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody2D))]
public class CarLogic : MonoBehaviour
{
public InputAction steeringAction;
public InputAction accelerationAction;
public InputAction brakeAction;
public InputAction steeringAction;
public InputAction accelerationAction;
public InputAction brakeAction;
public float accelerationAmount = 10;
[Tooltip("In degrees")]
public float steeringAnglePerSecond = 10;
public float accelerationAmount = 10;
[Tooltip("In degrees")]
public float steeringAnglePerSecond = 10;
public float maxAcceleration = 100;
private Rigidbody2D _rigidbody;
private Transform _transform;
private float _currentAcceleration;
public float maxAcceleration = 100;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody2D>();
_transform = transform;
}
private Rigidbody2D _rigidbody;
private Transform _transform;
private float _currentAcceleration;
private void Start()
{
steeringAction.Enable();
accelerationAction.Enable();
brakeAction.Enable();
}
private void Awake()
{
_rigidbody = GetComponent<Rigidbody2D>();
_transform = transform;
}
private void Update()
{
var steeringValue = steeringAction.ReadValue<float>();
var accelerationValue = accelerationAction.ReadValue<float>();
// TODO: The what that I changed this too I like a lot better, however I do want to eventually figure out what was wrong with this verbose approach.
// Vector2 velocityDir = _transform.up;
// var currentAngleRad = Mathf.Acos(Vector3.Dot(velocityDir, Vector3.up));
// var deltaAngleRad = steeringValue * steeringAnglePerSecond * Time.deltaTime * Mathf.Deg2Rad;
// var angleRad = deltaAngleRad + currentAngleRad;
//
// var angleCos = Mathf.Cos(angleRad);
// var angleSin = Mathf.Sin(angleRad);
// Vector2 nextVelocityDir;
// nextVelocityDir.x = angleCos * velocityDir.x -
// angleSin * velocityDir.y;
// nextVelocityDir.y = angleSin * velocityDir.x +
// angleCos * velocityDir.y;
//
// nextVelocityDir *= accelerationAmount * accelerationValue * Time.deltaTime;
private void Start()
{
steeringAction.Enable();
accelerationAction.Enable();
brakeAction.Enable();
}
// TODO: probably not correct... ODE or some other formula.
if (accelerationValue <= 0)
{
_currentAcceleration -= _rigidbody.drag * Time.deltaTime;
}
private void Update()
{
var steeringValue = steeringAction.ReadValue<float>();
var accelerationValue = accelerationAction.ReadValue<float>();
var brakeValue = brakeAction.ReadValue<float>();
// TODO: The way that I changed this too I like a lot better, however I do want to eventually figure out what was wrong with this verbose approach.
// Vector2 velocityDir = _transform.up;
// var currentAngleRad = Mathf.Acos(Vector3.Dot(velocityDir, Vector3.up));
// var deltaAngleRad = steeringValue * steeringAnglePerSecond * Time.deltaTime * Mathf.Deg2Rad;
// var angleRad = deltaAngleRad + currentAngleRad;
//
// var angleCos = Mathf.Cos(angleRad);
// var angleSin = Mathf.Sin(angleRad);
// Vector2 nextVelocityDir;
// nextVelocityDir.x = angleCos * velocityDir.x -
// angleSin * velocityDir.y;
// nextVelocityDir.y = angleSin * velocityDir.x +
// angleCos * velocityDir.y;
//
// nextVelocityDir *= accelerationAmount * accelerationValue * Time.deltaTime;
_currentAcceleration += accelerationValue * accelerationAmount * Time.deltaTime;
var newAngle = steeringValue * steeringAnglePerSecond * Time.deltaTime;
if (_currentAcceleration > 0.01)
{
_transform.Rotate(Vector3.forward, newAngle, Space.World);
}
// TODO: probably not correct... ODE or some other formula.
if (accelerationValue <= 0)
{
_currentAcceleration -= _rigidbody.drag * Time.deltaTime;
}
// TODO: reverse logic.
if (_currentAcceleration < 0)
{
_currentAcceleration = 0;
}
_currentAcceleration += accelerationValue * accelerationAmount * Time.deltaTime;
if (_currentAcceleration > maxAcceleration)
{
_currentAcceleration = maxAcceleration;
}
// TODO: probably want a brake amount, up to designers to make sure braking out performs acceleration, if desired.
_currentAcceleration -= brakeValue * accelerationAmount * Time.deltaTime * 2;
Debug.Log(_currentAcceleration);
_rigidbody.velocity = _transform.up * _currentAcceleration;
}
}
var newAngle = steeringValue * steeringAnglePerSecond * Time.deltaTime;
if (_currentAcceleration > 0.01)
{
_transform.Rotate(Vector3.forward, newAngle, Space.World);
}
// TODO: reverse logic.
if (_currentAcceleration < 0)
{
_currentAcceleration = 0;
}
if (_currentAcceleration > maxAcceleration)
{
_currentAcceleration = maxAcceleration;
}
_rigidbody.velocity = _transform.up * _currentAcceleration;
}
}