PROGRAMA DE AMOSTRA AUTOMOTRIZ
Tese: PROGRAMA DE AMOSTRA AUTOMOTRIZ. Pesquise 861.000+ trabalhos acadêmicosPor: sgbdfnjhkcd • 19/11/2014 • Tese • 1.980 Palavras (8 Páginas) • 244 Visualizações
// ----------- CAR TUTORIAL SAMPLE PROJECT, ? Andrew Gotow 2009 -----------------
// Here's the basic AI driven car script described in my tutorial at www.gotow.net/andrew/blog.
// A Complete explaination of how this script works can be found at the link above, along
// with detailed instructions on how to write one of your own, and tips on what values to
// assign to the script variables for it to work well for your application.
// -------------------------- Addapted and Modified by RCB ---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class MoveAICar : MonoBehaviour
{
// These variables allow the script to power the wheels of the car.
public WheelCollider frontLeftWheel;
public WheelCollider frontRightWheel;
// These variables are for the gears, the array is the list of ratios. The script
// uses the defined gear ratios to determine how much torque to apply to the wheels.
public float[] gearRatio;
public int currentGear = 0;
// Here's all the variables for the AI, the waypoints are determined in the "GetWaypoints" function.
// the waypoint container is used to search for all the waypoints in the scene, and the current
// waypoint is used to determine which waypoint in the array the car is aiming for.
public GameObject waypointContainer;
private List<Transform> waypoints;
private int currentWaypoint = 0;
// These variables are just for applying torque to the wheels and shifting gears.
// using the defined Max and Min Engine RPM, the script can determine what gear the
// car needs to be in.
public float engineTorque = 600F;
public float maxEngineRPM = 3000F;
public float minEngineRPM = 1000F;
private float engineRPM;
// input steer and input torque are the values substituted out for the player input. The
// "NavigateTowardsWaypoint" function determines values to use for these variables to move the car
// in the desired direction.
private float inputSteer = 0.0F;
private float inputTorque = 0.0F;
public void Start()
{
// This makes the car more stable
rigidbody.centerOfMass = new Vector3(rigidbody.centerOfMass.x,
-1.5F,
rigidbody.centerOfMass.z);
// Create the array with the gear ratios
gearRatio = new float[6];
gearRatio[0] = 4.31F;
gearRatio[1] = 2.71F;
gearRatio[2] = 1.88F;
gearRatio[3] = 1.41F;
gearRatio[4] = 1.13F;
gearRatio[5] = 0.93F;
// Call the function to determine the array of waypoints. This sets up the array of points by finding
// transform components inside of a source container.
FindWayPoints();
}
public void Update()
{
// This is to limith the maximum speed of the car
rigidbody.drag = rigidbody.velocity.magnitude / 250F;
// Call the funtion to determine the desired input values for the car. This essentially steers and
// applies gas to the engine.
NavigateTowardsWaypoint();
// Compute the engine RPM based on the average RPM of the two wheels, then call the shift gear function
engineRPM = (frontLeftWheel.rpm + frontRightWheel.rpm) / 2 * gearRatio[currentGear];
ShiftGears();
// Apply the values to the wheels. The torque applied is divided by the current gear, and
// multiplied by the user input variable.
frontLeftWheel.motorTorque = (engineTorque / gearRatio[currentGear]) * inputTorque;
frontRightWheel.motorTorque = (engineTorque / gearRatio[currentGear]) * inputTorque;
// The steer angle is an arbitrary value multiplied by the user input.
frontLeftWheel.steerAngle = 10 * inputSteer;
frontRightWheel.steerAngle = 10 * inputSteer;
}
private void ShiftGears()
{
...