Check if the player going right direction

Hello,
i want to add little helper to our racegame (like arrow or something), if the player is going wrong direction. Its two player splitscreen game. Attached image is overview of one level, and arrows represents desired direction; red for player1 and blue for player2.

I think this kind of thing can be done by checking player direction against some sort of direction vectors placed to the level. Not totally sure how to do that. Should i check if those two vectors intersect? Perhaps there is better way to accomplish this… any thoughts?

9781-directionhelper.jpg

Edit: checkpoint script added.

#pragma strict

static var curP1:String;
var nextP1:String;
static var _nextP1:String;

static var curP2:String;
var nextP2:String;
static var _nextP2:String;


function Start()
{
	curP1 = "";
	curP2 = "";
	
	_nextP1 = "start";
	_nextP2 = "start";
}


function OnTriggerEnter (other : Collider) 
{
    if ( other.gameObject.name == "Player1")
    {
    	curP1 = transform.name;
    
    	if (curP1 == _nextP1)
    		_nextP1 = nextP1;
    }
    
    if ( other.gameObject.name == "Player2")
    {
    	curP2 = transform.name;
    	
    	if (curP2 == _nextP2)
    		_nextP2 = nextP2;
    }
}

Idea is simple: every collider has a name and variable for next checkpoint. These needs to be edited in inspector, so i suppose i need to figure out better way to do this if i end up to use @CuddleMonster’s idea.

1 Answer

1

My Idea… assuming your cars/racers are a rigidbody: would be to have invisable boxes covering the entire track. They would be as wide as the track and as long as possible so long as there is no major curves in the track under them.

Each box would check to compare rigidbody.velocity (of your player) with the direction that the cube you are in says is forward. Have to run for now… Hopefully this helps.

PS. this could work for players going opposite directions by making a variable on the player that = rigidbody.velocity, except inversed on the players going in the opposite direction.

yep, upvoted. Use trigger colliders as waypoints. There is an excellent example here : http://www.youtube.com/watch?v=7NehsLWcFIU

Thanks for answer. At the moment i got just four checkpoints (+start) at the level; up,down,left,right. I'll try to add this method for those. Covering whole level with checkpoint system i have, would cause quite lot of work, so i suppose i should learn better way for checkpoints (waypoints), so thanks @alucardj for the example. I'll edit the question to include current checkpoint script.

And for others, here is the code to check direction: if (Vector3.Dot(transform.forward, playerGO.rigidbody.velocity) < 0){ print("Wrong direction"); }