add leon's carcontrol script
This commit is contained in:
parent
e76a04f73a
commit
d108cf47da
108
game/Assets/CarControl.cs
Normal file
108
game/Assets/CarControl.cs
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
using System.Xml.Schema;
|
||||||
|
using System;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class CarControls : MonoBehaviour
|
||||||
|
{
|
||||||
|
private float speed;
|
||||||
|
private float rotation;
|
||||||
|
public float MaxLeftSteer = -45;
|
||||||
|
public float MaxRightSteer = 45;
|
||||||
|
public float MaxForwardSpeed = 1024;
|
||||||
|
public float MaxBackwardSpeed = -16;
|
||||||
|
public float EngineBreakPower = 2f;
|
||||||
|
public float Acceleration = 1;
|
||||||
|
public float BreakForce = 10;
|
||||||
|
public float SteerStraighteningPower = 2f;
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
speed = 0;
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
float InRot = Input.GetAxis("Horizontal");
|
||||||
|
float InSp = Input.GetAxis("Vertical");
|
||||||
|
|
||||||
|
if(InRot==0)
|
||||||
|
{
|
||||||
|
if(speed!=0)
|
||||||
|
{
|
||||||
|
rotation -= rotation*SteerStraighteningPower*Time.deltaTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if((rotation>0 && InRot<0)||(rotation<0 && InRot>0))
|
||||||
|
rotation=0;
|
||||||
|
|
||||||
|
rotation += InRot;
|
||||||
|
}
|
||||||
|
|
||||||
|
rotation -= 0.1f*InRot*Time.deltaTime;
|
||||||
|
|
||||||
|
if(Input.GetAxis("Jump")>0)
|
||||||
|
{
|
||||||
|
if(speed>0)
|
||||||
|
{
|
||||||
|
speed -= BreakForce*Time.deltaTime;
|
||||||
|
speed = Mathf.Clamp(speed, 0, MaxForwardSpeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
speed += BreakForce*Time.deltaTime;
|
||||||
|
speed = Mathf.Clamp(speed, MaxBackwardSpeed, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Mathf.Abs(speed)<2f)
|
||||||
|
speed = 0;
|
||||||
|
}
|
||||||
|
else if(InSp == 0)
|
||||||
|
{
|
||||||
|
if(speed>0)
|
||||||
|
{
|
||||||
|
speed -= speed*EngineBreakPower*Time.deltaTime;
|
||||||
|
speed = Mathf.Clamp(speed, 0, MaxForwardSpeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
speed -= speed*EngineBreakPower*Time.deltaTime;
|
||||||
|
speed = Mathf.Clamp(speed, MaxBackwardSpeed, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(InSp>0)
|
||||||
|
{
|
||||||
|
speed += Acceleration;
|
||||||
|
}
|
||||||
|
else if(InSp<0)
|
||||||
|
{
|
||||||
|
speed -= Acceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(Mathf.Abs(speed)<0.1f)
|
||||||
|
speed=0;
|
||||||
|
|
||||||
|
if(Mathf.Abs(rotation)<0.1f)
|
||||||
|
rotation=0;
|
||||||
|
|
||||||
|
rotation = Mathf.Clamp(rotation, MaxLeftSteer, MaxRightSteer);
|
||||||
|
speed = Mathf.Clamp(speed, MaxBackwardSpeed, MaxForwardSpeed);
|
||||||
|
if(speed>0)
|
||||||
|
{
|
||||||
|
transform.Translate(UnityEngine.Vector3.forward * Mathf.Sqrt(speed) * Time.deltaTime);
|
||||||
|
transform.Rotate(0f,rotation*Time.deltaTime,0f);
|
||||||
|
}
|
||||||
|
else if (speed<0)
|
||||||
|
{
|
||||||
|
transform.Translate(UnityEngine.Vector3.back * Mathf.Sqrt(-speed) * Time.deltaTime);
|
||||||
|
transform.Rotate(0f,-rotation*Time.deltaTime,0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
game/Assets/CarControl.cs.meta
Normal file
11
game/Assets/CarControl.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1a379f6a4c0c4bda8afe838e061a30bd
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Reference in New Issue
Block a user