Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aal-ok · Jun 10, 2016 at 12:37 PM · inputtrigger

input button while in a trigger?

i am working on a fps game, and was making a switch to open some doors, i attached two colliders to the switch, one trigger which extends ahead of the trigger, to know whether the player is there or not, and other collider so that the player does not pass through it, it is placed on a wall, i can get the door to open when i enter the trigger, but the problem arises when i try to implement the door opening when the button e, is being pressed while the player is in the trigger, here is the script

public class Switch : MonoBehaviour { public GameObject door; public GameObject player;

 void OnTriggerEnter(Collider col)
 {
     if(col.gameObject.name=="Player")
     {
         if(Input.GetButtonDown("Use"))
         {    
             Debug.Log ("door open");
             door.transform.Translate (0, 5, 0);
         }
     }
 }

}

here's the script attached to the switch.

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

2 Replies

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

Answer by 5c4r3cr0w · Jun 10, 2016 at 12:46 PM

@aal-ok

You need to use OnTriggerStay() function instead of OnTriggerEnter().

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 aal-ok · Jun 10, 2016 at 03:53 PM 0
Share

thanks man! it worked, but can you please explain why? was it because ontriggerenter returns just when the target is entering?

avatar image 5c4r3cr0w aal-ok · Jun 11, 2016 at 04:43 PM 0
Share

Yes. For more information check out this tutorial.

https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers

avatar image
1

Answer by xXx12aithexXx · Jun 10, 2016 at 04:33 PM

I think a good alternative is to have the door have it's collider and its trigger..... like I think you're saying

but I would disreguard the ontrigger function...

instead I would make a "action raycast"

 using UnityEngine;
 using System.Collections;
 
 public class OpenDoorTest : MonoBehaviour {
 
     public float actionDistance = 100;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         RaycastHit rayhit;
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position,fwd,out rayhit,actionDistance)
         {
                 if (rayhit.collider.CompareTag("DoorTrigger")
                 {
                         if (Input.GetKeyDown("E"))
                         {
                                                   //Create a door script with function
                             bool openclose = false
 
                             openclose = !openclose;
                                                     rayhit.collider.gameObject.GetComponent<DoorFunctionScript>          ().opendoor = openclose;
                         }
                 }
         }
     
     }
 }


sorry for the bad code snippet.

Comment
Add comment · Show 1 · 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 aal-ok · Jun 12, 2016 at 04:44 AM 0
Share

so, you are saying to forget about the switch, yeah, that could work too, but i am not any good with raycasts, can you please explain a bit

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

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

Related Questions

Showing UI image when inside a trigger and then pressing a button 2 Answers

Problem with Holstering Sword in VR 0 Answers

Input trigger doesn't work 2 Answers

Double click detection for gamepad triggers? 0 Answers

Change light when in trigger and pressing "E" ? 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