Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using XInputDotNetPure;
- using UnityEngine.UI;
- /*Bug list/to do list
- - Make all coins disappear
- - car currently rolls sometimes when resetting the position of the car.
- - brakes might need tweaking
- - wheels don't spin yet
- - make more adjustments to make car feel better
- - add deadzone to the sticks
- - adjust controls
- */
- [System.Serializable]
- public class AxleInfo2 //easily addable extra wheels
- {
- public WheelCollider leftWheel;
- public WheelCollider rightWheel;
- public bool motor;
- public bool steering;
- public bool brakes;
- }
- public class CarController2 : MonoBehaviour
- {
- public List<AxleInfo2> axleInfos;
- public float maxMotorTorque;
- public float maxBrakeTorque;
- public float maxSteeringAngle;
- public float antirollbar = 1.0f; //helps to stop the car from rolling
- public Vector3 centreofmassOFFSET; //offsets the centre of mass to get a more realistic feel if needed
- public int PlayNum; //set this to 1, 2, 3 and 4 for separated controls.
- public float contTriggerDeadzone = 0.0f; // set between 0.0f-0.3f to set deadzone well.
- public int coins = 0; //the amount of coins the player has
- public bool enableInput = false; // turn input on/off
- public RawImage[] plus = new RawImage[11]; // displays +1 when a coin is collected
- public RawImage[] take = new RawImage[11]; // displays -1 when a trap is hit
- public int numToTake;
- public int numToDisp;
- public float TimePlus1OnScreen = 1.0f; //how long the +1 stays on screen
- public float downforce;
- public float boostPadIncrease = 10;
- //public for debugging
- private float motor;
- private float brake;
- private float steering;
- private bool starting = false;
- private Vector3 position;
- private Vector3 rotation;
- private float triggeroutput;
- private float wheelradius;
- private float wheelrpm;
- public void start()
- {
- }
- public void Update()
- {
- }
- public void FixedUpdate()
- {
- if(Input.GetKeyDown(KeyCode.Z))
- {
- displayNeg1();
- }
- if (Input.GetKeyDown(KeyCode.X))
- {
- displayPlus1();
- }
- if (starting == false) //startup not working, fixed using this bool
- {
- GetComponent<Rigidbody>().centerOfMass += centreofmassOFFSET; //offsets the centre of mass
- position = transform.position; //setup respawn
- rotation = transform.rotation.eulerAngles;
- foreach (AxleInfo2 axleInfo in axleInfos) //this helps to reduce weird wheelcollider jitters using settings found online
- {
- axleInfo.leftWheel.ConfigureVehicleSubsteps(5, 5, 1);
- axleInfo.rightWheel.ConfigureVehicleSubsteps(5, 5, 1);
- }
- starting = true;
- }
- if (enableInput) // checks if input is enabled
- {
- doInput();
- }
- else
- {
- motor = 0; // if it's not turn off the motor.
- //*maybe enable vehicle brakes here to prevent rolling still occuring
- }
- foreach (AxleInfo2 axleInfo in axleInfos) //apply steering, brakes and motor, get wheelradius and wheelrpm
- {
- wheelradius = axleInfo.rightWheel.radius;
- wheelrpm = axleInfo.leftWheel.rpm;
- rollBar(axleInfo);
- if (axleInfo.steering)
- {
- axleInfo.leftWheel.steerAngle = steering;
- axleInfo.rightWheel.steerAngle = steering;
- }
- if (axleInfo.motor)
- {
- axleInfo.leftWheel.motorTorque = motor;
- axleInfo.rightWheel.motorTorque = motor;
- }
- if (axleInfo.brakes)
- {
- axleInfo.leftWheel.brakeTorque = brake;
- axleInfo.rightWheel.brakeTorque = brake;
- }
- }
- }
- void doInput()
- {
- if (ControllerSetup.state[PlayNum].Triggers.Right >= contTriggerDeadzone
- && ControllerSetup.prevState[PlayNum].Buttons.LeftShoulder == ButtonState.Released) //drive forward if not pressing reverse button
- {
- motor = maxMotorTorque * ControllerSetup.state[PlayNum].Triggers.Right;
- }
- else
- {
- motor = 0;
- }
- if (ControllerSetup.state[PlayNum].Triggers.Left >= contTriggerDeadzone) //apply the brakes
- {
- brake = maxBrakeTorque * ControllerSetup.state[PlayNum].Triggers.Left;
- }
- else
- {
- brake = 0;
- }
- steering = maxSteeringAngle * ControllerSetup.state[PlayNum].ThumbSticks.Left.X; //steer car
- if (ControllerSetup.state[PlayNum].Buttons.LeftShoulder == ButtonState.Pressed
- && ControllerSetup.prevState[PlayNum].Buttons.LeftShoulder == ButtonState.Pressed) //reverse the car
- {
- motor = maxMotorTorque * -ControllerSetup.state[PlayNum].Triggers.Right;
- }
- if (Input.GetKeyDown(KeyCode.R)) // reset the car
- {
- GetComponent<Rigidbody>().velocity = Vector3.zero;
- GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
- transform.position = position;
- transform.localEulerAngles = rotation;
- }
- }
- void rollBar(AxleInfo2 axleInfo)
- {
- WheelHit hit;
- float leftwheelTravel = 1.0f; //set the wheel travel to fully extended
- float rightwheelTravel = 1.0f;
- bool leftwheelGrounded = axleInfo.leftWheel.GetGroundHit(out hit); //check if wheels are grounded
- bool rightwheelGrounded = axleInfo.rightWheel.GetGroundHit(out hit);
- if (leftwheelGrounded)//sets this to the actual wheel travel
- {
- leftwheelTravel = (-axleInfo.leftWheel.transform.InverseTransformPoint(hit.point).y - axleInfo.leftWheel.radius) / axleInfo.leftWheel.suspensionDistance;
- }
- else
- {
- leftwheelTravel = 1.0f;
- }
- if (rightwheelGrounded)
- {
- rightwheelTravel = (-axleInfo.rightWheel.transform.InverseTransformPoint(hit.point).y - axleInfo.rightWheel.radius) / axleInfo.rightWheel.suspensionDistance;
- }
- else
- {
- rightwheelTravel = 1.0f;
- }
- float antirollforce = (leftwheelTravel - rightwheelTravel) * antirollbar; // this is where we do what an antirollbar actually does and applies the stiffness
- if (leftwheelGrounded) //add the rollbar force to the wheels
- {
- GetComponent<Rigidbody>().AddForceAtPosition(axleInfo.leftWheel.transform.up * -antirollforce, axleInfo.leftWheel.transform.position);
- }
- if (rightwheelGrounded)
- {
- GetComponent<Rigidbody>().AddForceAtPosition(axleInfo.rightWheel.transform.up * -antirollforce, axleInfo.rightWheel.transform.position);
- }
- }
- public void resetpos() //reset the car
- {
- GetComponent<Rigidbody>().velocity = Vector3.zero;
- GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
- transform.position = position;
- transform.localEulerAngles = rotation;
- coins = 0;
- }
- public void displayPlus1() //display the +1
- {
- plus[1].enabled = true;
- coins += 1;
- Invoke("p1Disabled", TimePlus1OnScreen);
- }
- void p1Disabled()
- {
- plus[1].enabled = false;
- }
- public void displayNeg1() //display the -1
- {
- if (coins >= numToTake)
- {
- coins -= numToTake;
- numToDisp = numToTake;
- }
- else
- {
- numToDisp = coins;
- coins = 0;
- }
- take[numToDisp].enabled = true;
- StartCoroutine(Neg1Disabled());
- }
- IEnumerator Neg1Disabled()
- {
- int x = numToDisp;
- yield return new WaitForSeconds(TimePlus1OnScreen);
- take[x].enabled = false;
- }
- public void boostCar(Vector3 dir)
- {
- GetComponent<Rigidbody>().velocity += dir * boostPadIncrease;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement