• 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
Question by laurienash · Apr 29, 2013 at 09:06 PM · controlleractivateactivation

Only activate scripts when game is toggled to the first person controller

Hello, My game toggles between a first and third person controller. I have a script on a cube which I only want to work when the game is in first person, and not to work in third person.

This is the script:

 function OnMouseOver () 
 
 { rigidbody.velocity = transform.up * 05;
 
 yield WaitForSeconds (5);
 
 rigidbody.velocity = transform.up * 0;
 
 }

After looking around on the forums, I still can't work out how to change it so it is only activated in first person - I was wondering if anyone knew how to change it, or give me a pointer for what to look for?

Thanks so much, Laurien

Comment

People who like this

0 Show 2
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 whydoidoit · Apr 29, 2013 at 09:09 PM 0
Share

How are you making the change from First to Third person views?

avatar image laurienash · Apr 29, 2013 at 09:30 PM 0
Share

This is the script for toggling between views:

 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;                 // New check-variable
  
     //start with first person active
     function Start() {
        cam01.gameObject.active = true; 
        cam02.gameObject.active = false; 
        player02.GetComponent(CharacterController).active = false;
        check = true;
     }
  
  
     function Update() {
     
     player01.transform.position = player02.transform.position;
  
      if (Input.GetKeyDown ("return")) {
        if(check) {
          cam01.gameObject.active = false; 
          cam02.gameObject.active = true; 
          player01.GetComponent(CharacterController).active = false;
          player02.GetComponent(CharacterController).active = true;
        }
        else {
          cam01.gameObject.active = true; 
          cam02.gameObject.active = false; 
          player01.GetComponent(CharacterController).active = true;
          player02.GetComponent(CharacterController).active = false;
        }
     check = !check;
     }
     
     
   }

I tried to activate and deactivate the script on the cube this way, but it's not working.

 var firstPerson : GameObject;
 var thirdPerson : GameObject;
 
 
 
 function OnMouseOver () {
 
 if (firstPerson.active == true); {
 
  rigidbody.velocity = transform.up * 05;
 
 yield WaitForSeconds (5);
 
 rigidbody.velocity = transform.up * 0;
 }
 
 else { (firstPerson.active == false);
 
 rigidbody.velocity = transform.up * 0;
 
 }
 }

Thanks!

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Fornoreason1000 · Apr 29, 2013 at 10:24 PM

ok your deactivating Character Controllers but you aren't disabling the players themselves, so when you check firstperson.active it will always be true.

so for your cube script. use

if(firstperson.GetComponent(CharacterController).active == true)

instead of

firstperson.active

Comment

People who like this

0 Show 3 · 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 laurienash · May 01, 2013 at 03:47 PM 0
Share

Oh no - that's still giving me errors! Also - does that not apply to both character controllers, as there will always be either the first person or third person controller active, but I only want it to apply to the first person controller?

Changing it to the new line throws me this:

Assets/Game Scripts/Important Scripts/testOnMouseOverVelocity1.js(12,21): BCE0044: expecting :, found '='.

avatar image Fornoreason1000 · May 01, 2013 at 04:22 PM 0
Share

thats weird there nothing like that, Oh wait that a syntax error, you have Syntax errors. and a spelling mistake. dont worry fixed them

 var firstPerson : GameObject;
 var thirdPerson : GameObject;
  
  
  
 function OnMouseOver () {
 
 
     if (firstPerson.GetComponent(CharacterController).active) {
  
         rigidbody.velocity = transform.up * 10;
  
         yield WaitForSeconds (5);
  
         rigidbody.velocity = transform.up * 0;
     }
  
 else if (!firstPerson.GetComponent(CharacterController).active) {
  
     rigidbody.velocity = transform.up * 0;
  
 }
 }
avatar image laurienash · May 02, 2013 at 11:50 AM 0
Share

Wow thanks so much!!! That's really helped a lot :) (thanks)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to activate another Object with java script? 4 Answers

De-Activating object removes rigidbody velocity 1 Answer

Educational version 1 activation? 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