Acceleration improved
This commit is contained in:
parent
6b85258da0
commit
ec465e31a0
File diff suppressed because it is too large
Load Diff
83
Assets/Scripts/CameraOrbit.cs
Normal file
83
Assets/Scripts/CameraOrbit.cs
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
using System;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TDLN.CameraControllers
|
||||||
|
{
|
||||||
|
public class CameraOrbit : MonoBehaviour
|
||||||
|
{
|
||||||
|
public GameObject target;
|
||||||
|
public float distance = 10.0f;
|
||||||
|
|
||||||
|
public float xSpeed = 250.0f;
|
||||||
|
public float ySpeed = 120.0f;
|
||||||
|
|
||||||
|
public float yMinLimit = -20;
|
||||||
|
public float yMaxLimit = 80;
|
||||||
|
|
||||||
|
float x = 0.0f;
|
||||||
|
float y = 0.0f;
|
||||||
|
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
var angles = transform.eulerAngles;
|
||||||
|
x = angles.y;
|
||||||
|
y = angles.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
float prevDistance;
|
||||||
|
|
||||||
|
void LateUpdate()
|
||||||
|
{
|
||||||
|
if (distance < 2) distance = 2;
|
||||||
|
distance -= Input.GetAxis("Mouse ScrollWheel") * 2;
|
||||||
|
if (target && (Input.GetMouseButton(0) || Input.GetMouseButton(1)))
|
||||||
|
{
|
||||||
|
var pos = Input.mousePosition;
|
||||||
|
var dpiScale = 1f;
|
||||||
|
if (Screen.dpi < 1) dpiScale = 1;
|
||||||
|
if (Screen.dpi < 200) dpiScale = 1;
|
||||||
|
else dpiScale = Screen.dpi / 200f;
|
||||||
|
|
||||||
|
if (pos.x < 380 * dpiScale && Screen.height - pos.y < 250 * dpiScale) return;
|
||||||
|
|
||||||
|
// comment out these two lines if you don't want to hide mouse curser or you have a UI button
|
||||||
|
Cursor.visible = false;
|
||||||
|
Cursor.lockState = CursorLockMode.Locked;
|
||||||
|
|
||||||
|
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
|
||||||
|
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
|
||||||
|
|
||||||
|
y = ClampAngle(y, yMinLimit, yMaxLimit);
|
||||||
|
var rotation = Quaternion.Euler(y, x, 0);
|
||||||
|
var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
|
||||||
|
transform.rotation = rotation;
|
||||||
|
transform.position = position;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// comment out these two lines if you don't want to hide mouse curser or you have a UI button
|
||||||
|
Cursor.visible = true;
|
||||||
|
Cursor.lockState = CursorLockMode.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.Abs(prevDistance - distance) > 0.001f)
|
||||||
|
{
|
||||||
|
prevDistance = distance;
|
||||||
|
var rot = Quaternion.Euler(y, x, 0);
|
||||||
|
var po = rot * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
|
||||||
|
transform.rotation = rot;
|
||||||
|
transform.position = po;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static float ClampAngle(float angle, float min, float max)
|
||||||
|
{
|
||||||
|
if (angle < -360)
|
||||||
|
angle += 360;
|
||||||
|
if (angle > 360)
|
||||||
|
angle -= 360;
|
||||||
|
return Mathf.Clamp(angle, min, max);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/CameraOrbit.cs.meta
Normal file
11
Assets/Scripts/CameraOrbit.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e745887670e904825b07e4a8da7c38e6
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
|
|
@ -4,21 +4,36 @@ using UnityEngine;
|
||||||
|
|
||||||
public class CarController : MonoBehaviour
|
public class CarController : MonoBehaviour
|
||||||
{
|
{
|
||||||
public Wheel[] wheels;
|
public Wheel[] turningWheels;
|
||||||
|
public Wheel[] drivingWheels;
|
||||||
public Rigidbody rb;
|
public Rigidbody rb;
|
||||||
|
public float TopSpeed = 10;
|
||||||
|
|
||||||
|
public AnimationCurve PowerCurve;
|
||||||
|
public float EnginePower = 5;
|
||||||
|
|
||||||
void Awake(){
|
void Awake(){
|
||||||
foreach(Wheel wheel in wheels){
|
foreach(Wheel wheel in turningWheels){
|
||||||
|
wheel.myCar= this;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(Wheel wheel in drivingWheels){
|
||||||
wheel.myCar= this;
|
wheel.myCar= this;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
void Start()
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
|
||||||
void FixedUpdate()
|
void FixedUpdate()
|
||||||
{
|
{
|
||||||
|
//up or down
|
||||||
|
foreach(Wheel wheel in drivingWheels){
|
||||||
|
wheel.Rotate(Input.GetAxis("Vertical"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foreach(Wheel wheel in turningWheels){
|
||||||
|
wheel.Steer(Input.GetAxis("Horizontal"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@ public class Wheel : MonoBehaviour
|
||||||
public float restPosition;
|
public float restPosition;
|
||||||
public float restPositionOffset;
|
public float restPositionOffset;
|
||||||
public float springConstant = 1f;
|
public float springConstant = 1f;
|
||||||
|
public float tireGripFactor =1f;
|
||||||
|
public float tireMass = 1f;
|
||||||
|
public float steerAngle = 25;
|
||||||
public float dampningFactor = 1f;
|
public float dampningFactor = 1f;
|
||||||
public float velocity;
|
public float velocity;
|
||||||
// public ForceMode springForceMode;
|
// public ForceMode springForceMode;
|
||||||
|
|
@ -18,6 +21,7 @@ public class Wheel : MonoBehaviour
|
||||||
public float curDisplacement => restPosition - transform.localPosition.y;
|
public float curDisplacement => restPosition - transform.localPosition.y;
|
||||||
public float topMaxDisplacement => restPosition + displacementRange;
|
public float topMaxDisplacement => restPosition + displacementRange;
|
||||||
public float botMaxDisplacement => restPosition - displacementRange;
|
public float botMaxDisplacement => restPosition - displacementRange;
|
||||||
|
public float bottomEnd => transform.position.y - radius;
|
||||||
LayerMask groundMask;
|
LayerMask groundMask;
|
||||||
|
|
||||||
public CarController myCar;
|
public CarController myCar;
|
||||||
|
|
@ -30,17 +34,18 @@ public class Wheel : MonoBehaviour
|
||||||
}
|
}
|
||||||
public float rayDist ;
|
public float rayDist ;
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
|
bool grounded = false;
|
||||||
void FixedUpdate()
|
void FixedUpdate()
|
||||||
{
|
{
|
||||||
if (myCar == null) { return; }
|
if (myCar == null) { return; }
|
||||||
|
|
||||||
RaycastHit hit = new RaycastHit();
|
RaycastHit hit = new RaycastHit();
|
||||||
float newDisplacement = transform.localPosition.y;
|
float newDisplacement = transform.localPosition.y;
|
||||||
float bottomEnd = transform.position.y - radius;
|
|
||||||
Vector3 topEnd = transform.position + new Vector3(0, radius, 0);
|
Vector3 topEnd = transform.position + new Vector3(0, radius, 0);
|
||||||
|
|
||||||
bool grounded = false;
|
grounded=false;
|
||||||
if (Physics.Linecast(suspensionJoint.position, suspensionJoint.position - new Vector3(0,((radius*2)+(displacementRange))), out hit, groundMask))
|
if (Physics.Linecast(suspensionJoint.position, suspensionJoint.position -(transform.up * ((radius*2)+(displacementRange))), out hit, groundMask))
|
||||||
{
|
{
|
||||||
Debug.DrawLine(suspensionJoint.position, hit.point, Color.red);
|
Debug.DrawLine(suspensionJoint.position, hit.point, Color.red);
|
||||||
hitPoint = hit.point.y;
|
hitPoint = hit.point.y;
|
||||||
|
|
@ -49,17 +54,51 @@ public class Wheel : MonoBehaviour
|
||||||
CurDisplacement = restPositionOffset - rayDist;
|
CurDisplacement = restPositionOffset - rayDist;
|
||||||
velocity = Vector3.Dot(transform.up, myCar.rb.GetPointVelocity(transform.position));
|
velocity = Vector3.Dot(transform.up, myCar.rb.GetPointVelocity(transform.position));
|
||||||
force = (CurDisplacement * springConstant) - (velocity * dampningFactor);
|
force = (CurDisplacement * springConstant) - (velocity * dampningFactor);
|
||||||
myCar.rb.AddForceAtPosition(Vector3.up * force, transform.position);
|
myCar.rb.AddForceAtPosition(transform.up * force, transform.position);
|
||||||
grounded = true;
|
grounded = true;
|
||||||
|
Friction();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// transform.localPosition = Vector3.Lerp(transform.position,
|
transform.localPosition = Vector3.Lerp(transform.localPosition,
|
||||||
// new Vector3(transform.localPosition.x, botMaxDisplacement, transform.localPosition.z),0.2f);
|
new Vector3(transform.localPosition.x, botMaxDisplacement, transform.localPosition.z),0.2f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + velocity, transform.localPosition.z);
|
// transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + velocity, transform.localPosition.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Rotate(float input){
|
||||||
|
if(grounded ){
|
||||||
|
if(input >0){
|
||||||
|
|
||||||
|
}
|
||||||
|
Vector3 accelDir = transform.forward;
|
||||||
|
|
||||||
|
float carSpeed = Vector3.Dot(myCar.transform.forward, myCar.rb.velocity);
|
||||||
|
float normalizedSpeed= Mathf.Clamp01(Mathf.Abs(carSpeed) / myCar.TopSpeed);
|
||||||
|
float availableTorque = myCar.PowerCurve.Evaluate(normalizedSpeed) * input * myCar.EnginePower;
|
||||||
|
|
||||||
|
myCar.rb.AddForceAtPosition(accelDir * availableTorque, transform.position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Friction(){
|
||||||
|
if(grounded){
|
||||||
|
Vector3 steeringDir = transform.right;
|
||||||
|
Vector3 tireWorldVel = myCar.rb.GetPointVelocity(transform.position);
|
||||||
|
float steeringVel = Vector3.Dot(steeringDir, tireWorldVel);
|
||||||
|
float desiredVelChange = -steeringVel * tireGripFactor;
|
||||||
|
float desiredAccel = desiredVelChange / Time.fixedDeltaTime;
|
||||||
|
Vector3 friction = steeringDir * tireMass * desiredAccel;
|
||||||
|
// friction = new Vector3(friction.x, 0, friction.z);
|
||||||
|
myCar.rb.AddForceAtPosition(friction, transform.position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Steer(float input){
|
||||||
|
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, input * steerAngle, transform.localEulerAngles.z);
|
||||||
|
}
|
||||||
|
|
||||||
public float force;
|
public float force;
|
||||||
void OnDrawGizmos()
|
void OnDrawGizmos()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
m_EditorVersion: 2020.3.40f1
|
m_EditorVersion: 2020.3.34f1
|
||||||
m_EditorVersionWithRevision: 2020.3.40f1 (ba48d4efcef1)
|
m_EditorVersionWithRevision: 2020.3.34f1 (9a4c9c70452b)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user