Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Knightraven8 · Oct 03, 2012 at 11:40 PM · cameraplayerswitchsetactiverecursively

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"); 
 	} 
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Knightraven8 · Oct 03, 2012 at 10:32 PM 0
Share

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.

avatar image AlucardJay · Oct 03, 2012 at 11:46 PM 0
Share

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.

avatar image AlucardJay · Oct 04, 2012 at 12:04 AM 0
Share

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 =]

avatar image AlucardJay · Oct 04, 2012 at 03:52 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to switch between cameras/players? 1 Answer

I can't look up and down because of animation on camera? 2 Answers

How to set main camera? 3 Answers

C# - Offset In-Air Camera from Current Object Position 1 Answer

Input Controls Camera on a Predetermined Path. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges