- Home /
Speed car at the start of the game
I have a script.
/*
Script Created by FlatTutorials for "Car Controller kit".
*/
@script AddComponentMenu ("FlatTutorials/Scripts/Car Control Script")
#pragma strict
var centerOfMass : Vector3; //Center of mass
var dataWheel : WheelCollider; //Wheel Collider from which you want to calculate the speed
var lowestSteerAtSpeed : float = 50; //if lowestSteerAtSpeed < currentSpeed the steer Angle = highSpeedSteerAngel
var lowSpeedSteerAngel : float = 10; //This could be a high value
var highSpeedSteerAngel : float = 1; //This shouldn't be a high value (recomended for stability of car)
var decellarationSpeed : float = 30; //How fast the car will decellarate
var maxTorque : float = 50; //Maximum Torque
var currentSpeed : float; //Current Speed of car
var topSpeed : float = 150; //Highest speed at which the car can go
var maxReverseSpeed : float = 50; //Highest Reverse speed
var backLightObject : GameObject; //Mesh for reverse light
var idleLightMaterial : Material; //for idle state
var brakeLightMaterial : Material; //Braked state
var reverseLightMaterial : Material; //Reverse state
@HideInInspector
var braked : boolean = false; //Brake trigger
var maxBrakeTorque : float = 100; //Braking speed
var speedOMeterDial : Texture2D; //GUI Texture for dial
var speedOMeterPointer : Texture2D; //GUI Texture for needle
var gearRatio : int[]; //Shift gear at speed
function Start () {
rigidbody.centerOfMass=centerOfMass; //Center of mass , for this the car should be pointing on z axis
}
function FixedUpdate () {
HandBrake();
}
function Update(){
BackLight ();
EngineSound();
CalculateSpeed();
}
//Speed Calculation
function CalculateSpeed(){
currentSpeed = 2*22/7*dataWheel.radius*dataWheel.rpm*60/1000;
currentSpeed = Mathf.Round(currentSpeed);
}
//Light Control
function BackLight (){
if (currentSpeed > 0 && Input.GetAxis("Vertical")<0&&!braked){
backLightObject.renderer.material = brakeLightMaterial;
}
else if (currentSpeed < 0 && Input.GetAxis("Vertical")>0&&!braked){
backLightObject.renderer.material = brakeLightMaterial;
}
else if (currentSpeed < 0 && Input.GetAxis("Vertical")<0&&!braked){
backLightObject.renderer.material = reverseLightMaterial;
}
else if (!braked){
backLightObject.renderer.material = idleLightMaterial;
}
}
//Brake Trigger
function HandBrake(){
if (Input.GetButton("Jump")){
braked = true;
}
else{
braked = false;
}
}
//Engine Sound
function EngineSound(){
for (var i = 0; i < gearRatio.length; i++){
if(gearRatio[i]> currentSpeed){
break;
}
}
var gearMinValue : float = 0.00;
var gearMaxValue : float = 0.00;
if (i == 0){
gearMinValue = 0;
}
else {
gearMinValue = gearRatio[i-1];
}
gearMaxValue = gearRatio[i];
var enginePitch : float = ((currentSpeed - gearMinValue)/(gearMaxValue - gearMinValue))+1;
audio.pitch = enginePitch;
}
//Speedometer
function OnGUI (){
GUI.DrawTexture(Rect(Screen.width - 300,Screen.height - 150,300,150),speedOMeterDial);
var speedFactor : float = currentSpeed / topSpeed;
var rotationAngle : float;
if (currentSpeed >= 0){
rotationAngle = Mathf.Lerp(0,180,speedFactor);
}
else {
rotationAngle = Mathf.Lerp(0,180,-speedFactor);
}
GUIUtility.RotateAroundPivot(rotationAngle,Vector2(Screen.width - 150 ,Screen.height));
GUI.DrawTexture(Rect(Screen.width - 300,Screen.height - 150,300,300),speedOMeterPointer);
}
How to make a car when you turn on the game itself went forward? Start game = auto speed 100. How to do it?
Could you try and explain your question a little more in detail?
You've got the tail wagging the dog here. This script "shows" indicators...set the speedometer, turns on/off break lights, makes engine sounds. Nothing in this script is responsible for changing the speed of the vehicle. Your change will be in some other script.
Since this is not your script, this is likely copyrighted by someone else. Are you sure it is okay to post the author's scripts here?
I need to start the game when the car went forward. Sorry for my English. How do I make the car to run forward when I run the game. Start game = Car speed 100. When you turn on the game, the car had speed 100.
@Lucky12321 - As I mentioned, this is not the script you would make that change in.
Who publishes these kind of "questions" ? First of all this isn't question, this is "do work for me, even if i don't or can't understand anything". Plus, the user clearly have no idea or what so ever what "bla bla" is written there.
Answer by Sisso · Oct 03, 2013 at 09:40 PM
So you want to create a racing game? If so, this is my answer:
Your answer
Follow this Question
Related Questions
Car speedometer MPH 1 Answer
How to make Characters take Damage on speed Collision hit? 2 Answers
How to add car speed to a car controller script 1 Answer
How do I limit the speed of my car? 0 Answers
Car Steering Relative to Car Speed 2 Answers