- Home /
Vehicle/car/ Movement
//What's Wrong? I don't quite understand why my MovingF and MovingB won't turn false after not pushing "up" and "down" button. Also, my statement: currentSpeed = Acceleration * Time.deltaTime won't work. Why? Could anybody fix it please? :S
var Acceleration : float = 100;
var Deacceleration : float = 20;
var maxSpeed : float = 100;
@HideInInspector
var currentSpeed : float = 0;
var grounded : boolean = false;
var maxSlope : float = 60;
var Rotation = 50;
var MovingF : boolean = false;
var MovingB : boolean = false;
function Start ()
{
currentSpeed = 0;
MovingF = false;
MovingB = false;
}
function Update ()
{
currentSpeed = Acceleration * Time.deltaTime;
if (currentSpeed >= maxSpeed) {
currentSpeed = maxSpeed;
}
if (Input.GetKeyUp("up") == 0) {
if (grounded) {
if (MovingF == true) {
MovingF = false;
}
}
}
if (Input.GetKeyUp("down") == 0) {
if (grounded) {
if (MovingB == true) {
MovingB = false;
}
}
}
if(Input.GetKey("up")) {
if(grounded) {
transform.Translate(currentSpeed*Time.deltaTime, 0, 0);
MovingF = true;
}
}
if(Input.GetKey("down")) {
if(grounded) {
transform.Translate(-currentSpeed*Time.deltaTime, 0, 0);
MovingB = true;
}
}
if(Input.GetKey("left")) {
if(grounded) {
if(MovingF == true) {
transform.Rotate(0, -Rotation*Time.deltaTime, 0);
}
if(MovingB == true) {
transform.Rotate(0, Rotation*Time.deltaTime, 0);
}
}
}
if(Input.GetKey("right")) {
if(grounded) {
if(MovingF == true) {
transform.Rotate(0, Rotation*Time.deltaTime, 0);
}
if(MovingB == true) {
transform.Rotate(0, -Rotation*Time.deltaTime, 0);
}
}
}
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionExit ()
{
grounded = false;
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Car movement script ? 1 Answer
Space Invaders movement? 1 Answer
Problem with vehicle script 1 Answer
Bomberman movement script? 1 Answer