Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 William_Goodspeed · Apr 24, 2014 at 06:22 PM · animationcollisioncolliderdoor

Player to Door Collision and Animation

I could really use some help with this. I'm new to Unity scripting have previously been using just Visual Studio C# and I'm finding it difficult to get started.

Basically what I'm trying to do is have a door slide open when the player approaches it.

Now I've got my Capsule which is tagged as Player and I've got a Door which is tagged as MainDoor. Each of them has a collision box. The door has a Box Collider with Is Trigger ticked.

I've created my animation and called it DoorA and attached it to the Door object.

Now I've been trying to get the script working, at first I had it attached to the Door, then I had it attached to the player. What is confusing me is how to reference another objects collider and what it means when I'm calling a collider other?

Here is my code below:

 using UnityEngine;
 using System.Collections;
 
 public class MainDoor : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
     
     
     }
 
     void OnTriggerEnter(Collider other) {
         
         animation.Play ("DoorA");
 
     }
 
 
     // Update is called once per frame
     void Update () {
     
     }
 }

Now at present it is attached to the Door. In pseudo code this is what I think I should be doing

When Doors trigger is hit by Player Collider play door animation.

But I'm unsure how to do it! Help would be appreciate, I'm a novice!

Comment
Add comment · Show 1
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 William_Goodspeed · Apr 26, 2014 at 12:57 AM 0
Share

When I put a tag on the OnTriggerExit function I can see that it happens right away as soon as the game starts. Which suggest that the Player is leaving the trigger area on game launch. That doesn't make sense to me.

This script by the way is attached to the door not the player object.

3 Replies

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

Answer by Cherno · Apr 26, 2014 at 02:09 AM

Here is my door script, fairly comprehensive but you can just pick the parts you really need from it. You might have to set your door animation import setting so that the opening and closing animations are Clamp Forever, so CrossFade works (if the player opens it while it is closing). Also consider editing the physics layer interaction in Project Settings -> Physics so the door collider only interacts with player characters, or anything else that should block the door.

 using UnityEngine;
 using System.Collections;
 
 public class Door : MonoBehaviour
 {
 
     private int state;//0 = closed/idle, 1 = opening, 2 = closing, 3 = open
     private float openTimer;
     private float closeTimer;
     private float autoCloseTimer;
     public bool blocked;
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
 
         if(state == 3) {
 
             if(blocked == false) {
                 autoCloseTimer += Time.deltaTime;
             }
             else {
                 autoCloseTimer = 0.0f;
                 
             }
         }
         else {
 
             autoCloseTimer = 0.0f;
         }
 
         if(autoCloseTimer >= 5.0f && blocked == false) {
             ActivateDoor();
         }
 
         if(state == 1) {
             openTimer += Time.deltaTime;
 
         }
         if(openTimer >= 1.5f) {
             state = 3;
 
             ResetTimer();
         }
         if(state == 2) {
             closeTimer += Time.deltaTime;
             if(blocked == true) {
                 animation.CrossFade("door1_open", 1.0f, PlayMode.StopAll);
                 state = 1;
                 ResetTimer();
             }
 
         }
         if(closeTimer >= 1.5f) {
             state = 0;
 
             ResetTimer();
         }
     }
 
     void OnTriggerStay() {
         blocked = true;
                 ActivateDoor();
     }
 
     void OnTriggerExit() {
         blocked = false;
     }
 
     public void ActivateDoor() {
         if(state == 0) {
             animation.Play("door1_open", PlayMode.StopAll);
             state = 1;
 
             ResetTimer();
             return;
         }
         if(state == 1) {
             return;
         }
         if(state == 2) {
             animation.CrossFade("door1_open", 1.0f, PlayMode.StopAll);
             state = 1;
 
             ResetTimer();
             return;
         }
         if(state == 3) {
             animation.Play("door1_close", PlayMode.StopAll);
             state = 2;
 
             ResetTimer();
             return;
         }
 
     }
 
     
 
     void ResetTimer() {
         openTimer = 0.0f;
         closeTimer = 0.0f;
     }
 
     
 }
 
 
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 koray1396 · Apr 24, 2014 at 07:03 PM

I think you need;

 if(other.gameObject.tag == "Player"){
     animation.Play ("DoorA");
 }


In the below line, "other" is the Collider of the triggering object.

 void OnTriggerEnter(Collider other) {

So other.gameObject refers to the triggering gameObject. You could also be doing

 other.rigidbody.velocity = someSpeedVector;

therefore you would be reaching the rigidbody of the triggering object. Check the link below, you can see what variables you can use with Collider component.

https://docs.unity3d.com/Documentation/ScriptReference/Collider.html

Also as a note, your player should have a rigidbody in order to trigger occur. You can see from the below link, when collisions are detected.

http://docs.unity3d.com/Documentation/Components/class-MeshCollider.html

Comment
Add comment · Show 6 · 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 William_Goodspeed · Apr 24, 2014 at 07:32 PM 0
Share

Thank you, I managed to set that up. However now when I walk into the Trigger area the animation plays continiously. $$anonymous$$y code now looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ainDoor : $$anonymous$$onoBehaviour {
 
     private int count;
     // Use this for initialization
     void Start () {
 
     
     
     }
 
 
     void OnTriggerEnter(Collider other) {
 
         count++;
 
         if (other.gameObject.tag == "Player") {
 
                     animation.Play ("DoorA");
                 }
 
     }
 
     void OnTriggerExit(Collider other) {
 
 
         }
 
     // Update is called once per frame
     void Update () {
     
 
     }
 }

I have the "play automatically" button unchecked and I have set the animations Wrap $$anonymous$$ode to "Once". I'm not sure if this is because it is doing my animation code constantly as the player is stood inside the trigger area or because it is half in half out of the trigger area. I thought it would simply run that function once and the player would have to completely exit the area and re-enter for it to trigger again.

avatar image koray1396 · Apr 24, 2014 at 07:43 PM 0
Share

no, it should run once. I'm not sure why this is not working. While I think this should work as it is now, you can fix this, by defining another variable. It might be related to how you set up the colliders.

    if (other.gameObject.tag == "Player" && !isOpen) {
           animation.Play ("DoorA");
           isOpen = true;
       }


avatar image William_Goodspeed · Apr 25, 2014 at 01:34 AM 0
Share

I have managed to refine this code somewhat but with a new error!

So basically what I've done now is to attached an Animator Controller to the Door and in it I have two animations $$anonymous$$DClose and $$anonymous$$DOpen with transistions and a bool value attached to control the doors animation.

The problem is that the door opens when the player approaches as expect. However it does not shut after I leave the box collider.

Any thoughts?

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ainDoor : $$anonymous$$onoBehaviour {
 
     private int count;
 
     // Use this for initialization
     void Start () {
 
         anim = GetComponent<Animator> ();
     
     }
 
 
     void OnTriggerEnter(Collider other) {
 
         if (other.gameObject.tag == "Player") {
             count++;
 
                 } 
 
     }
 
     void OnTriggerExit(Collider other) {
 
 
                 if (other.gameObject.tag == "Player") {
                count = $$anonymous$$athf.$$anonymous$$ax (0, count-1);
 
                         }
 
         }
 
     // Update is called once per frame
     void Update () {
     
         anim.SetBool ("Open", count > 0);
 
 
 
     }
 }

alt text

controller.png (179.9 kB)
avatar image koray1396 · Apr 25, 2014 at 03:51 PM 0
Share

well, I can not see any reason why you are handling this like that. OnTriggerEnter you can use

 anim.SetBool ("Open", true);

OnTriggerExit;

 anim.SetBool ("Open", false);

you would not need to run this on every update.

Anyway, still in your script, if count goes up to 2 by a chance, then the door would not close. Put the following right under count++

Debug.Log(count);

and under this also. count = $$anonymous$$athf.$$anonymous$$ax (0, count-1);

you can check and see what's happening actually.

avatar image William_Goodspeed · Apr 25, 2014 at 11:33 PM 0
Share

Thank you for the help with this. It's such a simple task but it just doesn't seem to want to work.

I stripped my code back and use inputted the true and false you suggested within on on enter and on exit. However now when I run the game the door continuously opens and shuts, it's very strange.

Show more comments
avatar image
0

Answer by William_Goodspeed · Apr 28, 2014 at 05:50 PM

The above information was very helpful. As it turns out the problem with my code was that I had included a player controller which uses crouch. That crouch was kicking in when I approached the ceiling near the doors entrance and for whatever reason this was affective the collision and animation. I removed the crouch feature and it now works :)

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

22 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

Related Questions

FPSController Collision with moving objects 2 Answers

Collision occurring when it should not! Help! 1 Answer

Raycasting fail 1 Answer

Destroying object when player walks over it 1 Answer

Collider doesnt track with animation 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