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 /
  • Help Room /
avatar image
1
Question by dgoelitz · Apr 23, 2018 at 05:50 PM · collisioncollideranimator

Opening a door with OnCollisionStay

Hi, I'm trying to get a door to open by triggering a trigger parameter in the animator of the door when the character is colliding and the space bar is pressed. Nothing happens when I bring the character within the door's collider and press space. Maybe someone will have an idea for what I'm doing wrong. Thank you.`

using System.Collections; using System.Collections.Generic; using UnityEngine; public class DoorOpen : MonoBehaviour { Animator anim; int openHash = Animator.StringToHash("Open"); int closeHash = Animator.StringToHash("Close"); bool open = false; void Start() { anim = GetComponent<Animator>(); } void OnCollisionStay(Collision col) { if (Input.GetKeyDown("space")) { if (col.gameObject.name == "banker") { if (open == false) { anim.SetTrigger(openHash); open = true; } else { anim.SetTrigger(closeHash); open = false; } } } } }

Comment
Add comment · 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 vinilly · Apr 25, 2018 at 07:45 PM 0
Share

I am going to need a little more information on the animators? Are you on 3D or 2D?

avatar image dgoelitz vinilly · Apr 26, 2018 at 07:45 AM 0
Share

I'm on 2D.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dgoelitz · Apr 25, 2018 at 08:35 AM

@Captain_Pineapple @Remy_Unity and @Karsten you seem to be answering questions right now. Can either of you help me?

@ZuhairGhias @Harinezumi @keckluis you look like you're answering questions now. Can either of you help me?

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
1

Answer by vinilly · Apr 25, 2018 at 09:21 AM

You need to first call the Collider of col.gameObject.name before declaring any inputs.

You can try:

 void OnCollisionStay(Collision col)
 {
     if (col.gameObject.name == "banker" && Input.GetKeyDown("space"))
     {
         if (open == false)
         {
             anim.SetTrigger(openHash); open = true;
         }
         else
         {
             anim.SetTrigger(closeHash); open = false;
         }
     }
 }


Comment
Add comment · Show 7 · 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 dgoelitz · Apr 25, 2018 at 09:24 AM 0
Share

Unfortunately I can't try this right now but I will in the morning. Thank you so much.

avatar image vinilly dgoelitz · Apr 25, 2018 at 09:26 AM 0
Share

You're welcome I'll try to be faster next time. Good nite friend :D

avatar image dgoelitz · Apr 25, 2018 at 04:55 PM 0
Share

Doesn't work unfortunately. I'm testing it out with the animator open next to the game window and neither of the triggers ever get activated. Also I have two animations and whenever I select the door object with the second animation it switches back to the first one. Is it only possible to have one animation per object?

avatar image vinilly dgoelitz · Apr 25, 2018 at 08:12 PM 0
Share

If it is 2D simply call from script.

 public Sprite doorOpen, doorClose;
 
 void OnCollisionStay(Collision col)
  {
      if (col.gameObject.name == "banker" && Input.Get$$anonymous$$eyDown("space"))
      {
          if (open == false)
          {
              GetComponent<SpriteRenderer>().sprite = doorOpen;
              open = true;
              StartCouroutine(ClosingDoor());
          }
      }
  }
 
 private IEnumerator ClosingDoor()
     {
         yield return new WaitForSeconds(5);
         GetComponent<SpriteRenderer>().sprite = doorClose;
         open = false;
     }

This would Change the sprites better than an animator would for 2D.

avatar image dgoelitz vinilly · Apr 26, 2018 at 07:11 AM 0
Share

So then I need a sprite of the door opening ins$$anonymous$$d of an animation?

Show more comments

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

198 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 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 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 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

How to make OnTriggerEnter Only work for certain Gameobjects? 2 Answers

How to enable specific event when other gameobject collides with the other prefab? 0 Answers

Navmesh agents not colliding with non trigger colliders? 0 Answers

Why won't this object destroy on collision? 1 Answer

why isnt my code working, 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