Fixed some stuff, deceleration and brakes

This commit is contained in:
Sewmina 2022-10-07 19:23:41 +05:30
parent ec465e31a0
commit b517975659
13 changed files with 2507 additions and 42 deletions

View File

@ -0,0 +1,14 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!134 &13400000
PhysicMaterial:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: CarBody
dynamicFriction: 0.2
staticFriction: 0.2
bounciness: 0.2
frictionCombine: 1
bounceCombine: 3

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f5f62ad1de61740cb948d1b3f4d0fbda
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 13400000
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Materials.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c22a6d84027bfacb3bf0deeee91ec3d7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,78 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Black
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0cb668dc90bbc3962b375415d1da4a2e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,78 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Green
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.33574867, g: 0.4339623, b: 0.28862587, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0963ba8f8228610b39d3a2a102a6cc90
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

BIN
Assets/New Terrain.asset Normal file

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 554b5a552d9e5fafd87e33409b2b2934
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 15600000
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -30,7 +30,7 @@ namespace TDLN.CameraControllers
{
if (distance < 2) distance = 2;
distance -= Input.GetAxis("Mouse ScrollWheel") * 2;
if (target && (Input.GetMouseButton(0) || Input.GetMouseButton(1)))
if (target)
{
var pos = Input.mousePosition;
var dpiScale = 1f;

View File

@ -6,31 +6,47 @@ public class CarController : MonoBehaviour
{
public Wheel[] turningWheels;
public Wheel[] drivingWheels;
public List<Wheel> allWheels;
public Rigidbody rb;
public float TopSpeed = 10;
public float Deceleration;
public float BrakePower = 3;
public AnimationCurve PowerCurve;
public float EnginePower = 5;
public float curSpeed;
[Header("Steering")]
public float maxSteerAngle = 25;
public float minSteerAngle = 10;
void Awake(){
foreach(Wheel wheel in turningWheels){
wheel.myCar= this;
if(!allWheels.Contains(wheel)){allWheels.Add(wheel);}
}
foreach(Wheel wheel in drivingWheels){
wheel.myCar= this;
if(!allWheels.Contains(wheel)){allWheels.Add(wheel);}
}
}
void FixedUpdate()
{
//up or down
foreach(Wheel wheel in drivingWheels){
wheel.Rotate(Input.GetAxis("Vertical"));
}
if(Input.GetKeyDown(KeyCode.R)){transform.eulerAngles = new Vector3(0,0,0);}
curSpeed = Vector3.Dot(transform.forward, rb.velocity);
float verticalInput = Input.GetAxis("Vertical");
if((verticalInput >0 && curSpeed < 0) || (verticalInput <0 && curSpeed > 0)){
foreach(Wheel wheel in allWheels){
wheel.Brake();
}
}
foreach(Wheel wheel in drivingWheels){
wheel.Rotate(verticalInput);
}
foreach(Wheel wheel in turningWheels){
wheel.Steer(Input.GetAxis("Horizontal"));

View File

@ -11,7 +11,6 @@ public class Wheel : MonoBehaviour
public float springConstant = 1f;
public float tireGripFactor =1f;
public float tireMass = 1f;
public float steerAngle = 25;
public float dampningFactor = 1f;
public float velocity;
// public ForceMode springForceMode;
@ -38,65 +37,87 @@ public class Wheel : MonoBehaviour
void FixedUpdate()
{
if (myCar == null) { return; }
RaycastHit hit = new RaycastHit();
float newDisplacement = transform.localPosition.y;
Vector3 topEnd = transform.position + new Vector3(0, radius, 0);
grounded=false;
if (Physics.Linecast(suspensionJoint.position, suspensionJoint.position -(transform.up * ((radius*2)+(displacementRange))), out hit, groundMask))
if (Physics.Linecast(suspensionJoint.position, suspensionJoint.position -(transform.up * ((radius*2)+(displacementRange))), out hit))
{
Debug.DrawLine(suspensionJoint.position, hit.point, Color.red);
// Debug.DrawLine(suspensionJoint.position, hit.point, Color.red);
hitPoint = hit.point.y;
transform.position = new Vector3(suspensionJoint.position.x, hitPoint + (radius), suspensionJoint.position.z);
// transform.position = new Vector3(suspensionJoint.position.x, hitPoint + (radius), suspensionJoint.position.z);
rayDist = suspensionJoint.position.y - hitPoint;
transform.position = suspensionJoint.position - (suspensionJoint.up *(rayDist - (radius)));
CurDisplacement = restPositionOffset - rayDist;
velocity = Vector3.Dot(transform.up, myCar.rb.GetPointVelocity(transform.position));
force = (CurDisplacement * springConstant) - (velocity * dampningFactor);
if(force < 0){force=0;}
myCar.rb.AddForceAtPosition(transform.up * force, transform.position);
Debug.DrawLine(suspensionJoint.position, transform.position + (transform.up * force), Color.red);
grounded = true;
Friction();
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition,
new Vector3(transform.localPosition.x, botMaxDisplacement, transform.localPosition.z),0.2f);
Vector3 newPosition = suspensionJoint.position - (suspensionJoint.up * ((radius)+(displacementRange)));
transform.position = Vector3.Lerp(transform.position, newPosition, 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);
}
public void Rotate(float input){
if(grounded ){
if(input >0){
Vector3 accelDir = transform.forward;
if(input ==0){
//Decelerate
Vector3 force = accelDir * -myCar.curSpeed/myCar.Deceleration;
myCar.rb.AddForceAtPosition(force, suspensionJoint.position);
Debug.DrawLine(suspensionJoint.position, suspensionJoint.position + force, Color.red);
}else{
float normalizedSpeed= Mathf.Clamp01(Mathf.Abs(myCar.curSpeed) / myCar.TopSpeed);
float availableTorque = myCar.PowerCurve.Evaluate(normalizedSpeed) * input * myCar.EnginePower;
Vector3 force = accelDir * availableTorque;
myCar.rb.AddForceAtPosition(force, suspensionJoint.position);
Debug.DrawLine(suspensionJoint.position, suspensionJoint.position + force, Color.blue);
}
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);
}
}
public void Brake(){
if(!grounded){return;}
Vector3 accelDir = transform.forward;
Vector3 force = accelDir * -myCar.curSpeed/myCar.BrakePower;
myCar.rb.AddForceAtPosition(force, suspensionJoint.position);
Debug.DrawLine(suspensionJoint.position, suspensionJoint.position + force, Color.red);
}
void Friction(){
if(grounded){
Vector3 steeringDir = transform.right;
Vector3 tireWorldVel = myCar.rb.GetPointVelocity(transform.position);
Vector3 tireWorldVel = myCar.rb.GetPointVelocity(suspensionJoint.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);
Debug.DrawLine(suspensionJoint.position, suspensionJoint.position + friction, Color.blue);
myCar.rb.AddForceAtPosition(friction, suspensionJoint.position);
}
}
public void Steer(float input){
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, input * steerAngle, transform.localEulerAngles.z);
float carSpeed = Mathf.Clamp01(Vector3.Dot(myCar.transform.forward, myCar.rb.velocity) / myCar.TopSpeed);
float newSteerAngle = myCar.maxSteerAngle - ((myCar.maxSteerAngle - myCar.minSteerAngle) * carSpeed);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, input * newSteerAngle, transform.localEulerAngles.z);
}
public float force;