Jump to content
  • Advertisement
Sign in to follow this  
DzoniGames

Unity [JavaScript] -- Unity Collision Problem

This topic is 869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

 

I've been working on a racing game and everything was going pretty well except the collision. I had a problem with the car hitting a wall and still going forward ( physics ) so I fixed that and now the car bounced of the wall, but only when you release the move key. I tried fixing that and it worked. Then I started testing it a bit and for some reason, sometimes when going at high speed, the car would glitch out and would lose its collider and obviously it would not have any collision what so ever and it would go twice as fast until it reaches a speed of zero.

Here is all the necessary code:

Note: I have deleted some code to save up space?


var exhaustNormal : GameObject;
var exhaustNitro : GameObject;

var nitro : boolean = false;
var nitroTime : float = 15;


static var speedView : int;

static var speed : float = 0;

static var lastDir : String;
var lastCol : String;


var maxSpeed : float = 45;

var acceleration : float;
var deacceleration : float = 20;

var isMovingForward : boolean = false;
var isMovingBackward : boolean = false;
var isMoving : boolean = false;


static var collidedForw : boolean = false;
static var collidedBack : boolean = false;

var braking : boolean = false;
var brakingRot : boolean = false;

var canMoveForw : boolean = true;
var canMoveBack : boolean = true;
var pressingBoth : boolean = false;


var fpA : boolean = true;
var fp1A : boolean = false;
var fp2A : boolean = false;
var finish : boolean = false;

function Start() {

	speed = 0;
	maxSpeed = 55;
	nitroTime = 15;
	nitro = false;
	braking = false;
	canMoveForw = true;
	canMoveBack = true;
	collidedForw = false;
	collidedBack = false;
	pressingBoth = false;
}

function Update() {

	if(Input.GetAxis("Forward") && Input.GetAxis("Backward")) {

		pressingBoth = true;
		deaccelerate();
	} else {

		pressingBoth = false;
	}

	if(Input.GetAxis("Nitro") && isMovingBackward == false && isMovingForward == true && started1 == true && nitroTime >= 0) {

		if(nitroTime >= 0) {

			nitroTime -= 3 * Time.deltaTime;
			nitro = true;
		}
	}
	if(!(Input.GetAxis("Nitro"))) {

		nitro = false;
	}
	if(nitro == false && turningLeft == false && turningRight == false) {

		maxSpeed = 55;
	}
	if(nitro == true) {

		if(turningLeft == true || turningRight == true) {

			maxSpeed = 58;
		} else {
				
			maxSpeed = 60;
		}
	}
	if(nitroTime <= 0) {

		nitro = false;
	}

	transform.position.y = 0;


	if(speedView <= 60) {

		if(nitro == true) {

			acRot = 3;
			acceleration = 15;
		} else {

			acRot = 2;
			acceleration = 10;
		}
	}
	if(speedView > 60 && speedView < 120) {

		if(nitro == true) {

			acRot = 5;
			acceleration = 20;
		} else {

			acRot = 3;
			acceleration = 15;
		}
	}
	if(speedView > 120 && speedView < 180) {

		if(nitro == true) {

			acRot = 7;
			acceleration = 25;
		} else {

			acRot = 5;
			acceleration = 20;
		}
	}
	if(speedView > 180) {

		if(nitro == true) {

			acRot = 9;
			acceleration = 30;
		} else {

			acRot = 7;
			acceleration = 25;
		}
	}
	if(lastCol == "") {

		if(speedView <= 60) {

			if(braking == false) {

				deacceleration = 20;
			} else {

				deacceleration = 25;
			}
		}
		if(speedView > 60 && speedView < 120) {

			if(braking == false) {

				deacceleration = 15;
			} else {

				deacceleration = 20;
			}
		}
		if(speedView > 120 && speedView < 180) {

			if(braking == false) {

				deacceleration = 10;
			} else {

				deacceleration = 15;
			}
		}
		if(speedView > 180) {

			if(braking == false) {

				deacceleration = 5;
			} else {

				deacceleration = 10;
			}
		}
	} else {

		deacceleration = 30;
	}

	if(Input.GetAxis("Forward") && isMovingBackward == false && started1 == true) {

		if(lastDir == "backward" && collidedBack == false) {

			deaccelerate();
			braking = true;
			canRotLeft = false;
			canRotRight = false;
		} if(!(lastDir == "backward") && canMoveForw == true) {

			lastDir = "forward";

			accelerate();

			isMoving = true;
			isMovingForward = true;
			isMovingBackward = false;
			canRotLeft = true;
			canRotRight = true;

			col();

			transform.Translate(Vector3(1, 0, 0) * speed * Time.deltaTime);
		}
	}
	if(!(Input.GetAxis("Forward"))) {

		isMovingForward = false;
	}

	if(!(Input.GetAxis("Backward"))) {

		isMovingBackward = false;
	}

	if(Input.GetAxis("Backward") && isMovingForward == false && started1 == true) {

		if(lastDir == "forward" && collidedForw == false) {

			deaccelerate();
			braking = true;
			canRotLeft = false;
			canRotRight = false;
		} if(!(lastDir == "forward") && canMoveBack == true) {

			lastDir = "backward";

			accelerate();

			isMoving = true;
			isMovingBackward = true;
			ifMovingForward = false;
			breakingBack = false;
			canRotLeft = true;
			canRotRight = true;

			col();

			transform.Translate(Vector3(1, 0, 0) * -speed * Time.deltaTime);
		}
	}

	if(isMoving == true && nitro == false) {

		exhaustNormal.SetActive(true);
		exhaustNitro.SetActive(false);
	}
	if(isMoving == true && nitro == true) {

		exhaustNormal.SetActive(false);
		exhaustNitro.SetActive(true);
	}
	if(isMoving == false) {

		exhaustNormal.SetActive(false);
		exhaustNitro.SetActive(false);
	}

	if(isMovingForward == false && isMovingBackward == false) {

		isMoving = false;
	}

	if(!(Input.GetAxis("Left"))) {

		isRotingLeft = false;
	}

	if(!(Input.GetAxis("Right"))) {

		isRotingRight = false;
	}

	if(Input.GetAxis("Left") && lastDir == "forward" && speed > 0 && canRotLeft == true && braking == false && isRotingRight == false) {

		if(lastRot == "right") {

			deacRott();
			brakingRot = true;
		} else {

			isRoting = true;
			isRotingLeft = true;
			lastRot = "left";
			goLeft();
			acRott();
		}
	}


	if(turningLeft == true || turningRight == true) {

		if(nitro == false) {
	
			maxSpeed = 54;
		}
	}

	if(lastDir == "forward" && speed > 0) {

		if(isMoving == false) {

			transform.Translate(Vector3(1, 0, 0) * speed * Time.deltaTime);
		}
		if(lastCol == "backward") {

			transform.Translate(Vector3(1, 0, 0) * speed * Time.deltaTime);
		}
	}

	if(lastDir == "backward" && speed > 0) {

		if(isMoving == false) {

			transform.Translate(Vector3(1, 0, 0) * -speed * Time.deltaTime);
		}
		if(lastCol == "forward") {

			transform.Translate(Vector3(1, 0, 0) * -speed * Time.deltaTime);
		}
	}

	if(lastRot == "right" && isRoting == false && speedRot > 0) {

		rotating += speedRot * 10 * Time.deltaTime;

		transform.eulerAngles = Vector3(-90, 0, rotating);
	}

	if(lastRot == "left" && isRoting == false && speedRot > 0) {

		rotating -= speedRot * 10 * Time.deltaTime;

		transform.eulerAngles = Vector3(-90, 0, rotating);
	}

	if(speed <= 0) {

		isMovingBackward = false;
		isMovingForward = false;
		canMoveForw = true;
		canMoveBack = true;
		collidedForw = false;
		collidedBack = false;
		lastDir = "";
		lastCol = "";
		speed = 0;
	}


	speedView = speed * 4;
	Mathf.RoundToInt(speedView);


	if(collidedForw == false && collidedBack == false) {

		canMoveBack = true;
		canMoveForw = true;
	}
}

function col() {

	if(collidedBack == true && collidedForw == false) {

		braking = true;
		canMoveBack = false;
	}

	if(collidedForw == true && collidedBack == false) {

		braking = true;
		canMoveForw = false;
	}
}

function OnCollisionEnter(col: Collision) {

	if(col.gameObject.tag == "wall") {

		if(collidedForw == true && lastDir == "forward") {

			lastDir = "backward";
			lastCol = "forward";
		}
		if(collidedBack == true && lastDir == "backward") {

			lastDir = "forward";
			lastCol = "backward";
		}
	}
}

Share this post


Link to post
Share on other sites
Advertisement

Ok, so I guess nobody bothered to read 400 lines of code or nobody could figure it out but here's how I fixed it:

At the part where it checks what is the lastDir and if speed is greater than 0 I just added for forward to check if lastCol == "forward" and for backward to check if lastCol == "backward" So that it check for both the back and front if the player didn't click any other button. I also added so that the deacceleration of the car increases by quite a bit.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!