Prototype of a racing game.
Need to add art via Kenny.io's sprite packs. Could also add checkpoints and lap logic to make it feel like a game and less of a prototype.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class CarLogic : MonoBehaviour
|
||||
{
|
||||
public InputAction steeringAction;
|
||||
public InputAction accelerationAction;
|
||||
public InputAction brakeAction;
|
||||
|
||||
public float accelerationAmount = 10;
|
||||
[Tooltip("In degrees")]
|
||||
public float steeringAnglePerSecond = 10;
|
||||
|
||||
public float maxAcceleration = 100;
|
||||
|
||||
private Rigidbody2D _rigidbody;
|
||||
private Transform _transform;
|
||||
private float _currentAcceleration;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rigidbody = GetComponent<Rigidbody2D>();
|
||||
_transform = transform;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
steeringAction.Enable();
|
||||
accelerationAction.Enable();
|
||||
brakeAction.Enable();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// TODO: probably not correct... ODE or some other formula.
|
||||
if (accelerationValue <= 0)
|
||||
{
|
||||
_currentAcceleration -= _rigidbody.drag * 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: reverse logic.
|
||||
if (_currentAcceleration < 0)
|
||||
{
|
||||
_currentAcceleration = 0;
|
||||
}
|
||||
|
||||
if (_currentAcceleration > maxAcceleration)
|
||||
{
|
||||
_currentAcceleration = maxAcceleration;
|
||||
}
|
||||
|
||||
Debug.Log(_currentAcceleration);
|
||||
_rigidbody.velocity = _transform.up * _currentAcceleration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d73a8141f49460f4c834eabcc50990eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Follow : MonoBehaviour
|
||||
{
|
||||
public Transform target;
|
||||
|
||||
public Vector3 offset;
|
||||
|
||||
private Transform _transform;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_transform = transform;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_transform.position = target.position + offset;
|
||||
_transform.rotation = target.rotation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6c2f84ec7cb9d94eba89eedfc505c45
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user