- Home /
Two player cannonball scene, need scripting help
I created a scene that has two castles each made of up a few hundred rigidbody bricks, and a flag. Players take turns firing various cannonballs from their cannons at the other players castle, trying to hit the flag. I'm having a bit of difficulty transitioning between players after the shots.
Basically what I'm trying to do is after each shot (pressing space) have the game switch between two cameras positioned slightly off screen of each other, switch the active player, and simultaneously activate and deactivate each players castle as it switches turns.
This is what I have. It almost works but for some reason it switches the active player twice every other time the spacebar is pressed, and I can't figure out why. Any help would be appreciated.
	if (Input.GetKeyDown(KeyCode.Space)){
		// press space to shoot
		var ball: GameObject = null;
		if (cannonballType == "Heavy Cannonball") {
			if (numHeavy > 0){
				ball = Instantiate(heavyball, shotVector, Quaternion.identity);
				numHeavy--;
				print(numHeavy + " Heavy Cannonballs remaining");
			}
			else {
				print("No Heavy Cannonballs remaining");
			}
		}
		else {
			ball = Instantiate(cannonball, shotVector, Quaternion.identity);
		}
		Debug.Log(dir);
		Debug.Log(activePlayer);
		PlayerSwitch();
		ball.rigidbody.velocity = Vector3(0,angle,- power);
		Destroy(ball, 10);
		//print("Angle:" + angle + " Power:" + power);
	}
}
function PlayerSwitch(){
	//checks to see which player is currently active
	if (activePlayer == "Player 1"){
		yield WaitForSeconds (6);
		//time delay to allow the player to watch the cannonball
		//play. Eventually I will add something to stop the player from firing extra
		//cannonballs during this time
		activePlayer = "Player 2"; //changes active player to new active player
		Debug.Log(activePlayer);
		dir = cannon2.transform.position.x; //sets the X vector of Vector3 to Player's
		cannon's X vector
		Debug.Log(dir);
		camera1.active = false; //deactivates Player 1's camera
		player1.SetActiveRecursively(false); //turns off Player's castle/cannon
		camera2.active = true; //activates Player's camera
		player2.SetActiveRecursively(true); //turns on Player's castle/cannon
		print(activePlayer + "p"); //Print's active player
	} else {
		yield WaitForSeconds (6);
		activePlayer = "Player 1";
		Debug.Log(dir);
		dir = cannon1.transform.position.x;
		Debug.Log(activePlayer);
		camera2.active = false;
		player2.SetActiveRecursively(false);
		camera1.active = true;
		player1.SetActiveRecursively(true);
		print(activePlayer + "p");
	}
}
I'm not really sure why this post keeps posting this way. I have tried this about five times now, and have gone as far as formatting the code in a notepad file and then copying it, but it still keeps posting in this weird way. sorry.
To format text as code, just select the text which should be formatted as code and press the "101 010" button.
You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting.
When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it.
Hi, I just formatted your code (sounds like it was a stressful time, I've been there). It looks like function Update() got lost at the top though (no function was at the top, am assu$$anonymous$$g t was meant to be in Update). Good Luck =]
What happened to that lovely code formatting I did ?!
Delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting.
Answer by MickM · Oct 04, 2012 at 01:52 AM
It may be due to this line
if (Input.GetKeyDown(KeyCode.Space)){
If the space button is held down for more than one frame it will run it repeatedly... try swap it with:
if (Input.GetKeyUp(KeyCode.Space)){
(That will only run it when the space button is lifted) Not sure if it will be the magic bullet that fixes it but hopefully it helps!!!