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 Gabestronaut · Oct 21, 2013 at 04:04 AM · collisioncollidersontriggerenterbooleantriggers

OnTriggerEnter Multiple Collisions Activating. I tried many variations, Please help (Java)

Well, I'm making a game where its different game types all in one. Its a massive team project for school and I am the only programmer,I literally learned Java in 5 weeks by myself scrounging through here However, I cant get my code to work properly. In the picture provided, I have two Colliders. What I want my script to do is when I press e and collied with one, it will switch me to one controller, when I collide with the other box,it will move me to the second character controller. However, when I collided with either, it just moves me to the same character controller and in addition, when I press play I can press play and my camera automatically switches. I have to move at least once before it limits the key press to the colliders. I tried seperate scripts, variables, so I tried to be as specific as I can with tags. My boolean doesnt seem to work properly, anyway to limit which collisions to specific names?alt text Any help will be awesome. Thanks.

 //First Collider Script
 
 var change:boolean = false;
 
 //var PlayerCam: GameObject;
 
 //var NodeCam: GameObject;
 
 //var KeyCam: GameObject;
 
 //var NodeTrigger: GameObject;
 
 var GameTypeTrigger: GameObject;
 
 
 function OnTriggerEnter(col: Collider){
 //Function onTriggerEnter(OnTriggerEnter is called when the Collider "coll" other enters the trigger and activates-
 //the function update below) 
     change = true; 
  //(activate = true;) refer to the variable playerCam:boolean to be active or true)  S
    }
 
 
 function OnTriggerExit(col:Collider){
 // OnTriggerExit is called when the Collider other has stopped touching the trigger.
   change = false;
 //VariableCameraName.UnityCameraObject.Enabled/Disbaled = Activate
 }
 
 
 
 
 
 function Update(){
      
      
     if(Input.GetKeyDown("e") && change){
    Debug.Log("switch ready");
    
       GameObject.FindWithTag("playerCam").camera.enabled = false;
           //Disables FirstPerson Camera
       GameObject.FindWithTag("keyCam").camera.enabled = false;
       
       GameObject.FindWithTag("nodeCam").camera.enabled = true;
           //Enables Node Camera
       
       GameObject.FindWithTag("nodeCharacter").GetComponent("TopDownController").enabled = true;
           //Enables Node Control
       GameObject.FindWithTag("firstPersonCharacter").GetComponent("FPSInputController").enabled = false;
           //Disables FirstPerson Control
       GameObject.FindWithTag("keyCharacter").GetComponent("CharacterController").enabled = false;
           //Disables Platformer Functionality
     
     }
 
 }

 //Second Collider Script
 var activate:boolean = false;
 
 //var PlayerCam: GameObject;
 
 //var KeyCam: GameObject;
 
 //var NodeCam:GameObject;
 
 var PlatformerTrigger: GameObject;
 
 //var GameTypeTrigger: GameObject;
 
 function OnTriggerEnter(col: Collider){
 //Function onTriggerEnter(OnTriggerEnter is called when the Collider "coll" other enters the trigger and activates-
 //the function update below) 
     activate = true; 
  //(activate = true;) refer to the variable playerCam:boolean to be active or true)  S
    }
 
 
 function OnTriggerExit(col:Collider){
 // OnTriggerExit is called when the Collider other has stopped touching the trigger.
   activate = false;
 //VariableCameraName.UnityCameraObject.Enabled/Disbaled = Activate
 }
 
 
 
 
 
 function Update(){
      
      
     if(Input.GetKeyDown("e") && activate){
    Debug.Log("switch ready");
 
     GameObject.FindWithTag("playerCam").camera.enabled = false;
           //Disables FirstPerson Camera
      GameObject.FindWithTag("keyCam").camera.enabled = true;
       
       GameObject.FindWithTag("nodeCam").camera.enabled = false;
           //Enables Node Camera
 
       GameObject.FindWithTag("firstPersonCharacter").GetComponent("FPSInputController").enabled = false;
           //Disables FirstPerson Control
       GameObject.FindWithTag("nodeCharacter").GetComponent("TopDownController").enabled = false;
           //Disables BombDiffusal Functionality
       GameObject.FindWithTag("keyCharacter").GetComponent("CharacterController").enabled = true;
           //Enables Platformer Functionality
     }
 }
 

example.jpg (74.1 kB)
Comment
Add comment · Show 6
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 Benproductions1 · Oct 21, 2013 at 04:05 AM 0
Share

How did you implement the Java V$$anonymous$$ in Unity?

avatar image Gabestronaut · Oct 21, 2013 at 04:21 AM 0
Share

I dont understand what you mean by Java V$$anonymous$$, in anycase, it probably means I did not implement it :/

avatar image Benproductions1 · Oct 21, 2013 at 07:40 AM 0
Share

JAVA is a program$$anonymous$$g language that runs on it's own Virtual $$anonymous$$achine. It is also the program$$anonymous$$g language that $$anonymous$$inecraft and a couple other games were made in. It absolutely terrible for games due to certain performance issues and it's completely incompatible with Unity. I was just wondering how you managed to run Java in Unity, and how you taught Java to run Javascript code???

avatar image Gabestronaut · Oct 21, 2013 at 03:29 PM 0
Share

Lol what? I have Unity 3.5 idk what version you are using but when you right click in the inspector, it says create new Javascript running on $$anonymous$$ono Develop. Unity's engine is preset to run Java so that is why I am a bit confused as to why you asked how I got it to work. Its been working for me thus far. I created a small FPS Alpha and it works fine its just a bit buggy because you have to be ridiculously specific from what I've found. Everything has been reference up until the point where I've failed so much through trial and error that I can now look at the code and understand it. All I am trying to do now is OnEnterTrigger check for collision with a specific object's tag and I cannot for the love of Scripting figure it out >:/

avatar image toborific · Oct 21, 2013 at 03:40 PM 0
Share

Java and Javascript are two very different things. Unity allows scripting in Javascript. I wonder if you are confusing Java with Javascript.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toborific · Oct 21, 2013 at 03:46 PM

You said you wanted OnTriggerEnter to check for collision with a specific object's tag. This is how it should work:

 void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "PickUp"){
                         Debug.Log("You collided with a Pickup");
             other.gameObject.SetActive(false);
 
         }
         
 
         
         if(other.gameObject.tag == "Finish"){
             winText.text = "YOU WIN!";
         }
     
     }


Here, "other" is the collider that contacted the trigger. You can access its tag via other.gameObject.tag

Comment
Add comment · 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 Gabestronaut · Oct 21, 2013 at 03:58 PM 0
Share

Here is the thing though toborific, in my code I have a collider (col) in my OnTriggerEnter, is there a way to call it in my "If" paramaters for function update? All I keep getting is an error for if(Input.Get$$anonymous$$eyDown("e") && change && col.gameObject.tag("blah");

Unity says it doesnt recognize (col) even though I already declared it in my ontriggerenter

I have a boolean to check for collision named (change) so thats why I am calling it there, tried to tag that and I get some really weird error

avatar image Gabestronaut · Oct 21, 2013 at 04:29 PM 0
Share

Oh toborific you are right, however I meant for it to check on my function update. On$$anonymous$$yTriggerEnters I have a collider (col) and Im trying to call for it with a tag in my function update's if statement. However, when I col.tag("nodeTrigger) in the the update's if statemtent I get an error saying there is no such thing as (col) if(Input.Get$$anonymous$$eyDown("e") && activate && col.tag("nodeTrigger)== Error also tried activate.tag and no instance of activate exists. Perhaps I am looking at this all wrong?

avatar image toborific · Oct 21, 2013 at 04:54 PM 0
Share

Yeah, Update() doesn't have access to your player collider like that. I think what you want to do is put your if/then in the OnTriggerEnter and OnTriggerExit:

function OnTriggerExit(col:Collider){ // OnTriggerExit is called when the Collider other has stopped touching the trigger. if(col.gameObject.tag == "nodeTrigger"){ activate = false; }

//VariableCameraName.UnityCameraObject.Enabled/Disbaled = Activate }

avatar image
0

Answer by Gabestronaut · Oct 23, 2013 at 03:36 AM

Toborific I Found a workaround thanks to your first OnTriggerEnter Solution however, I did not know you can apply a collider setting to an OnTrigger thinking it was unity preset and not modifiable . Thanks! I appreciate it! Here is my Workaround But I will try what you provided now

 function OnTriggerEnter(other: Collider) {
         if(other.gameObject.tag == "nodeTrigger") 
         {
         Debug.Log("Working!");
         
       GameObject.FindWithTag("nodeCharacter").GetComponent("TopDownController").enabled = true;
           //Enables Node Control
       GameObject.FindWithTag("Player").GetComponent("FPSInputController").enabled = false;
           //Disables FirstPerson Control
       GameObject.FindWithTag("keyCharacter").GetComponent("CharacterController").enabled = false;
           //Disables Platformer Functionality
       GameObject.FindWithTag("playerCam").camera.enabled = false;
           //Disables FirstPerson Camera
       GameObject.FindWithTag("keyCam").camera.enabled = false;
       
       GameObject.FindWithTag("nodeCam").camera.enabled = true;
           //Enables Node Camera
         }
         
         else if (other.gameObject.tag == "keyTrigger") {
         Debug.Log("otheroneisWorking!");
         
         GameObject.FindWithTag("nodeCharacter").GetComponent("TopDownController").enabled = false;
           //Enables Node Control
       GameObject.FindWithTag("Player").GetComponent("FPSInputController").enabled = false;
           //Disables FirstPerson Control
       GameObject.FindWithTag("keyCharacter").GetComponent("CharacterController").enabled = true;
           //Disables Platformer Functionality
       GameObject.FindWithTag("playerCam").camera.enabled = false;
           //Disables FirstPerson Camera
       GameObject.FindWithTag("keyCam").camera.enabled = true;
       
       GameObject.FindWithTag("nodeCam").camera.enabled = false;
           //Enables Node Camera
         }
    
         }
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
avatar image
0

Answer by DubT · Nov 21, 2014 at 05:44 AM

i think one of the problems with the first code

  if(Input.GetKeyDown("e") && change){
 Debug.Log("switch ready");

was 'change' not knowing if it should be true or false ? should be

  if(Input.GetKeyDown("e") && change == true){
 Debug.Log("switch ready");

///////////////////////////////////////////////////////////////////////

here is fixed version i hope =)

  //First Collider Script
 var change:boolean = false;
 //var PlayerCam: GameObject;
 //var NodeCam: GameObject;
 //var KeyCam: GameObject;
 //var NodeTrigger: GameObject;
 var GameTypeTrigger: GameObject;
 function OnTriggerEnter(col: Collider){
 //Function onTriggerEnter(OnTriggerEnter is called when the Collider "coll" other enters the trigger and activates-
 //the function update below)
 if(col.tag == "YOURTAG")
 {
 change = true;
 }
 //(activate = true;) refer to the variable playerCam:boolean to be active or true) S
 }
 function OnTriggerExit(col:Collider){
 // OnTriggerExit is called when the Collider other has stopped touching the trigger.
 if(col.tag == "YOURTAG")
 {
 change = false;
 }
 //VariableCameraName.UnityCameraObject.Enabled/Disbaled = Activate
 }
 function Update(){
 if(Input.GetKeyDown("e") && change == true){
 Debug.Log("switch ready");
 GameObject.FindWithTag("playerCam").camera.enabled = false;
 //Disables FirstPerson Camera
 GameObject.FindWithTag("keyCam").camera.enabled = false;
 GameObject.FindWithTag("nodeCam").camera.enabled = true;
 //Enables Node Camera
 GameObject.FindWithTag("nodeCharacter").GetComponent("TopDownController").enabled = true;
 //Enables Node Control
 GameObject.FindWithTag("firstPersonCharacter").GetComponent("FPSInputController").enabled = false;
 //Disables FirstPerson Control
 GameObject.FindWithTag("keyCharacter").GetComponent("CharacterController").enabled = false;
 //Disables Platformer Functionality
 }
 }
  
 //Second Collider Script
 var activate:boolean = false;
 //var PlayerCam: GameObject;
 //var KeyCam: GameObject;
 //var NodeCam:GameObject;
 var PlatformerTrigger: GameObject;
 //var GameTypeTrigger: GameObject;
 function OnTriggerEnter(col: Collider){
 //Function onTriggerEnter(OnTriggerEnter is called when the Collider "coll" other enters the trigger and activates-
 //the function update below)
 if(col.tag == "YOURTAG")
 {
 activate = true;
 }
 //(activate = true;) refer to the variable playerCam:boolean to be active or true) S
 }
 function OnTriggerExit(col:Collider){
 // OnTriggerExit is called when the Collider other has stopped touching the trigger.
 if(col.tag == "YOURTAG")
 {
 activate = false;
 }
 //VariableCameraName.UnityCameraObject.Enabled/Disbaled = Activate
 }
 function Update(){
 if(Input.GetKeyDown("e") && activate == false){
 Debug.Log("switch ready");
 GameObject.FindWithTag("playerCam").camera.enabled = false;
 //Disables FirstPerson Camera
 GameObject.FindWithTag("keyCam").camera.enabled = true;
 GameObject.FindWithTag("nodeCam").camera.enabled = false;
 //Enables Node Camera
 GameObject.FindWithTag("firstPersonCharacter").GetComponent("FPSInputController").enabled = false;
 //Disables FirstPerson Control
 GameObject.FindWithTag("nodeCharacter").GetComponent("TopDownController").enabled = false;
 //Disables BombDiffusal Functionality
 GameObject.FindWithTag("keyCharacter").GetComponent("CharacterController").enabled = true;
 //Enables Platformer Functionality
 }
 }
 

///////////////////////////////////////////////////////

I think that may fix it but you would have to change 'YOURTAG' to the correct names, let me know if you care hahahah

Comment
Add comment · Show 2 · 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 Gabestronaut · Dec 09, 2014 at 05:57 AM 0
Share

@DubT Hey I reprogrammed the Entire Game a long time ago when I got a bit better afterwards and sorry for the last response. I have not been on this in a while and just saw this

$$anonymous$$y real issue was all those tags were just too messy and not specific and I still had no idea how to use gameObject variables but its all good now. Thank You ALL THO I appreciate it alot.

Here's what we were able to do with it if anyone is interested in the results!

https://www.youtube.com/watch?v=iFUUaAF9Sbk

avatar image Khaeops · Jul 19, 2016 at 07:23 AM 0
Share

You do not need to use "== true" or "== false" when checking for booleans. You can simply use the boolean's name to check.

In this instance:

 if (change)

is the same as:

 if (change == true)

Putting a ! in front of your boolean will also read as false. This is just an easier way to clean up code.

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

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

Related Questions

get only one colliding body 3 Answers

Why Trigger colliders costs more than non trigger ones? 0 Answers

How do I ignore trigger objects for collision? 0 Answers

On Trigger enters being called a frame late 1 Answer

How do I create AI with a good side and evil side? 0 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