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 laurienash · Jun 10, 2013 at 12:47 PM · collisionswitchfirst-person-controller

Switching between controllers - why do I have to press spacebar twice before it works?

Hello - in my game the player can switch between first and third person on pressing the space bar. Once the third person controller hits a collider, after 3 seconds it switches to the first person controller automatically without the player pressing space.

The script to SwitchBetweenControllers works, and the one to ForcePlayerIntoFirstPerson works - but my problem is that once the player is in first person - he has to press the spacebar twice before switching to the third person controller.

I can't work out why this is - any help would be so much appreciated!

This is the script for SwitchBetweenControllers (JavaScript):

  var cam01 : GameObject; // first person camera
     var cam02 : GameObject; // third person camera
     var player01 : GameObject; //first person controller
     var player02 : GameObject; //third person controller
     var check;                 //  check-variable
  
     //start with first person active
     function Start() {
        cam01.gameObject.active = true; 
        cam02.gameObject.active = false; 
        player02.active = false;
        check = true;
        
 
     }
  
  
     function Update() {
     
     player01.transform.position = player02.transform.position;
  
      if (Input.GetKeyDown ("space")) {
        if(check) {
          cam01.gameObject.active = false; 
          cam02.gameObject.active = true; 
          player01.active = false;
          player02.active = true;
        }
        else {
          cam01.gameObject.active = true; 
          cam02.gameObject.active = false; 
          player01.active = true;
          player02.active = false;
        }
     check = !check;
     }
     
     
   }


And this is the script for ForcePlayerIntoFirstPerson (which is attached to the collider):

 var cam01 : GameObject; // first person camera
 var cam02 : GameObject; // third person camera
 var player01 : GameObject; //first person controller
 var player02 : GameObject; //third person controller
 var check;
  
 function OnTriggerEnter(other: Collider){
 
   if (other.tag == "Player")
   { 
 
 yield WaitForSeconds (3);
 
 cam01.gameObject.active = true;
 cam02.gameObject.active = false;
 player01.active = true;
 player02.active = false;
 check = true;
 
 }
 
  
 }

Thanks so much, Laurien

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SubatomicHero · Jun 10, 2013 at 01:16 PM

OK so if they are two separate scripts, there are a couple of issues with your boolean checks.

  1. In your ForcePlayerIntoFirstPerson script, you don't need a local variable called "check". You need to get access to the check variable in your SwitchBetweenControllers script. So your OnTriggerEnter() should be updated to this:

    function OnTriggerEnter(other: Collider) { if(other.tag == "Player"){ yield WaitForSeconds(3);

          cam01.gameObject.active = true;
             cam02.gameObject.active = false;
             player01.active = true;
             player02.active = false;
             var temp : SwitchBetweenControllers = gameObject.GetComponent(SwitchBetweenControllers);
             temp.check = true;
         }
     }
    
    

Then back in your ForcePlayerIntoFirstPerson script in your update function this maybe a bit neater:

 function Update() {
     if (Input.GetKeyUp("space")) {
         if (check) check = false;
         else check = true;
     }
 
     if(check) {
         cam01.gameObject.active = false; 
         cam02.gameObject.active = true; 
         player01.active = false;
         player02.active = true;
     } else {
         cam01.gameObject.active = true; 
         cam02.gameObject.active = false; 
         player01.active = true;
         player02.active = false;
     }
 }

See if this helps!

Comment
Add comment · Show 5 · 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
avatar image Owen-Reynolds · Jun 10, 2013 at 01:53 PM 0
Share

The OP had the line check=!check; which is rewritten here as the long way: if(check) check=false; else check=true;. Now, maybe the OP didn't know what !check meant, so this is better.

In other words, the only change in Update is moving "flip check" up, to before it gets used.

avatar image SubatomicHero · Jun 10, 2013 at 01:57 PM 0
Share

No I did, I was just trying to simplify it as much as possible :P

avatar image laurienash · Jun 10, 2013 at 02:17 PM 0
Share

I've changed the line check = !check -- but I'm still having to press twice to go back to the third person controller, I can't work out why.

(I tried the new codes - but the second one came up with a lot of errors, and the switch between controllers stopped working properly)

avatar image Owen-Reynolds · Jun 10, 2013 at 05:37 PM 0
Share

Sorry. I just made it worse. The point is that the "new code" is really just 3 little changes to the original code.

One of the changes is just to move up check=!check. subAtH also rewrote that with an if, so you might be able to understand it better.

avatar image laurienash · Jun 12, 2013 at 12:42 PM 0
Share

In the ForcePlayer into first person - I just added the extra line of script - it does seem like it should work, but I'm still having to press the spacebar twice.

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

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Waypoints using a Switch... 2 Answers

OnCollisionEnter doesnt seem to work, i want to make objects dissappear when i come in contact with them? 2 Answers

Boolean check not referenced correctly from other script 2 Answers

Make object A parent of object b 1 Answer

OnCollision Script... 2 Answers


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