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
2
Question by ScriptGirl · Oct 19, 2011 at 04:30 PM · colliderontriggerentergetkey

GetKeyUp inside a collider only? How?

My question is a really simple one but at this stage it has really got me stuck! How can I add the OnTriggerEnter to my script so that it only activates the GetKeyUp when inside a collider?

I have two controllable characters (for simplicity I will call them Player A & Player B). Player B has a rigidbody applied, a small cube parented to it with a box collider and parenting script (for triggering when Player A is close enough to get parented to Player B). Further I have the below script attached to both players which allows me to change controls Player A ---> Player B using GetKeyUp "2", Player B ---> Player A using GetKeyUp "1" (if Player A is inside the small cube when pressing "2", since they were parented to Player B, they will now move alongside Player B after the controller switch). Here is the script I am using:

 // add your player and camera
     var PlayerA : GameObject;
     var PlayerB : GameObject;
     var PlayerACam : GameObject;
     var PlayerBCam : GameObject;
        
 function Start (){
     PlayerA.active = true;
     PlayerB.active = false;
     PlayerACam.camera.enabled = true;
     PlayerBCam.camera.enabled = false;
 }
     
 function Update (){
 // Hit 1 key to PlayerA playable
     if(Input.GetKeyUp("1")){
     PlayerA.active = true;
     PlayerB.active = false;
     PlayerACam.camera.enabled = true;
     PlayerBCam.camera.enabled = false;
 }
    
 // Hit 2 key to PlayerB playable
     if(Input.GetKeyUp("2")){
     PlayerB.active = true;
     PlayerA.active = false;
     PlayerBCam.camera.enabled = true;
     PlayerACam.camera.enabled = false;
 }
 }

Both players have this script attached to them since when one gets disabled, the other needs to be able to re-enable them. My understanding is that only Player B would need to have the OnTriggerEnter function added since it will be the one with an attached trigger collider. I am really stuck though as to how to implement this into the script :(

So, for clarity my question here again: How can I add the OnTriggerEnter to my script so that it only activates the GetKeyUp when Player A is inside Player B's collider?

Any help at this point would be greatly appreciated. Thank you, and looking forward to a reply.

Comment
Add comment · Show 3
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 Pilot · Oct 19, 2011 at 04:52 PM 1
Share

FYI: try to have titles which are clear and specific about your problem

avatar image ScriptGirl · Oct 19, 2011 at 04:54 PM 0
Share

fixed. thanks

avatar image Pilot · Oct 20, 2011 at 10:27 AM 0
Share

I have found the following links here and here, but both of the questions are unresolved. I have tried integrating the scripts but I always get a number of errors. $$anonymous$$aybe the answers are outdated. Any progress from your side? I can't believe there isn't a single answer here to something so simple and quite commonly used :(

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by gfr · Oct 20, 2011 at 10:55 AM

One simple approach would be setting a flag:

var insideZone : boolean = false;

function OnTriggerEnter (other : Collider) { if (/ check for tag or something /) { insideZone = true; } }

function OnTriggerExit (other : Collider) { if (/ check for tag or something /) { insideZone = false; } }

function Update() { if(insideZone && Input.GetKeyUp("1")) { // ...

Comment
Add comment · Show 22 · 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 wccrawford · Oct 20, 2011 at 11:13 AM 0
Share

I'm sure you meant OnTriggerExit for that second function.

avatar image Pilot · Oct 20, 2011 at 03:47 PM 1
Share

Could the problem be from the fact that you are using OnTriggerEnter (which calls the function only during the frame when the player enters the collider) as oppose to OnTriggerStay or OnCollisionStay which would call the function during the entire time that the player is inside the vehicle trigger box? Allowing the player the ability to press the "2" key as long as the collision was occurring.

avatar image Pilot · Oct 20, 2011 at 05:36 PM 1
Share

It worked! Thank you @Hamesh81 for providing the link and @ScriptGirl for piecing the ideas together, and of course a big thank you to @gfr for his huge help also.

When pressing 2 outside the collider it does nothing. When entering the collider and pressing 2 it changes controls. There remains a problem however with the final function. For some reason the debug.log keeps returning "2 was pressed" whether you are inside or outside the collider. Shouldn't it return this only when both conditions are met?

avatar image aldonaletto · Oct 20, 2011 at 05:48 PM 1
Share

Debug.Log is outside the if: move it right after PlayerACam.camera... and it will work as expected.

avatar image Hamesh81 · Oct 21, 2011 at 04:36 AM 2
Share

Glad you guys managed to get it working!! Gd to see you kept at it despite what others said :)

Btw, I'm not expecting any credit since I just gave you guys a few links, but I'm glad they helped. I think Pilot and gfr did most of the work anyway. Cheers

Show more comments
avatar image
2

Answer by aldonaletto · Oct 20, 2011 at 03:28 PM

The whole idea has a problem: if you set PlayerB.active to false, it disappears and its trigger doesn't work anymore. If you want PlayerB to disappear, replace it with an invisible cube to serve as the trigger. If you just want PlayerB to stay static and doesn't respond to controls while PlayerA is active, it's better to disable PlayerB scripts instead. Anyway, using different scripts in each player is better than using the same script, since both have different behaviours.

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 ScriptGirl · Oct 20, 2011 at 03:42 PM 0
Share

When PlayerB.active is set to false the trigger DOES still work. Since when they aren't active and PlayerA enters the trigger they are automatically parented to PlayerB via a script attached to the trigger. Using my original script, when key 2 is pressed the controls can then be switched and PlayerB will be able to move with PlayerA parented to them.

I don't understand why I would want to make a vehicle disappear or be invisible? And as mentioned in the original question, each player GO has a separate script attached to them which is how one can turn off and enable the other.

avatar image Hamesh81 · Oct 20, 2011 at 04:13 PM 2
Share

@aldonaletto: Are you sure you read the question, because it seems you have misunderstood what ScriptGirl & Pilot are trying to achieve here.

@ScriptGirl & @Pilot: I have managed to find the following link which you might find useful. It's a bit outdated but uses OnTriggerStay in a similar way to what you are trying to do, you might find it helpful: here is the link.

avatar image aldonaletto · Oct 20, 2011 at 05:33 PM 0
Share

@ScriptGirl, setting a game object active to false deactivates all of its components, including collider and renderer, what deactivates trigger detection and makes the object invisible. Ok, I understand now that the trigger volume is a parent (or child?) of it, but still can't understand how the deactivated object will stay visible.
But never $$anonymous$$d: if this approach is working for you, go ahead!

avatar image
1

Answer by Pilot · Oct 21, 2011 at 12:03 PM

The original script needs to be combined with the script provided by gfr. The collider in question needs to be tagged to differentiate it from the other gameobjects. Accordingly, in the if statement of the script, the same tag needs to be called in order to check whether in fact it is the player that is colliding with the relevant collider. Use the links posted by Hamesh81 and myself if you are having trouble writing this part of the script. The update function only needs to contain the if statement for switching to the other gameobject (since you don't need to switch to the gameobject which is already active).

Use the debug.log as gfr suggested in order to test specific parts of the script. Also note that while the script used here is quite basic, there are many situations where things can go wrong, so I would not recommend trying this if you are a complete beginner. However with care and patience even beginners should be able to pull this off. Hope this answer is helpful to anyone trying to do similar things to this.

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 DavidDebnar · Oct 20, 2011 at 05:38 PM

So when I understand it, what you wanna do is make a player go into a vehicle. You could either Raycast, check for distance or check for collision. Ill make a simple version of your script.

 var player : Transform; //sorry but I use transforms more often and I'm used to em :).
 var vehicle : Transform;
 
 function Start()
 {
   //disable the vehicle here, but not the whole thing. Disable just the scripts and controllers on it, don't disable the collider/renderer (I suspect, that you wanna have your vehicle visible)
 }
 
 function Update()
 {
  //I will script all 3 versions
  Ver1();
  /*Ver2();*/
 }
 
 function Ver1()
 {
  var hit : RaycastHit;
  if(Physics.Raycast(transform.position, transform.forward, hit, 1)) //check if something is 1 meter forward
   if(hit.collider.tag == "vehicle" && Input.GetKeyDown(KeyCode.1))
    HopIn();
 }
 
 function Ver2()
 {
  if(Vector3.Distance(player.position,vehicle.position) <= 1 && Input.GetKeyDown(KeyCode.1))
   HopIn();
 }
 
 /*function OnTriggerEnter(hit : collider) //Ver3
 {
  if(hit.collider.tag == "vehicle" && Input.GetKeyDown(KeyCode.1))
   HopIn();
 }
 */
 function HopIn()
 {
  //make the vehicle enabled here, and the player disabled/parented to it
 }

  • David

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

7 People are following this question.

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

Related Questions

OnTriggerEnter not recognising collision ?(Solved) 3 Answers

Trigger OnTriggerEnter Not Triggering Properly on Collision 0 Answers

Why isnt OnTriggerEnter Tags working. 1 Answer

OnTriggerEnter only working twice... 0 Answers

how do i correctly use a box collider 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