• 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
Question by Jack423 · May 21, 2014 at 01:33 PM · animationcollisionaudiotrigger

Play Animation OnTriggerEnter

I have a door script that is supposed to play an animation when I walk in the trigger and then also play a door opening sound. I have the same thing but except it is OnTriggerExit and it plays the sound and lays a close animation. The problem is, is that the sound is playing only on the trigger functions, but none of the animations are playing.

 #pragma strict
 
 var Door : GameObject;
 var OpenSound : AudioClip;
 var CloseSound : AudioClip;
 
 function OnTriggerEnter(Other : Collider)
 {
     if(Other.gameObject.tag == "Player")
     {
         Door.animation.Play("SlidingDoor_Open");
         audio.clip = OpenSound;
         audio.Play();
     }
 }
 
 function OnTriggerExit(Other : Collider)
 {
     if(Other.gameObject.tag == "Player")
     {
         Door.animation.Play("SlidingDoor_Close");
         audio.clip = CloseSound;
         audio.Play();
     }
 }    
Comment

People who like this

0 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 Sprawl · May 21, 2014 at 01:43 PM 0
Share

Did you make sure that the animations were working beforehand ? If the sounds are getting called then the problem isn't the trigger.

avatar image KiraSensei · May 21, 2014 at 02:35 PM 0
Share

Does your door game object has animations ? You need to check that in the inspector.

avatar image Klarax · May 21, 2014 at 02:50 PM 0
Share

a) check object is not static - made this mistake in the past b) look into layering the aminations in the start function.

i will have a look later at my project about layers and post a snippet of code that does what you need. email me at klarax (at) gmail (dot) com if not heard from me. i have a tendency to forget :P

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by LazyDog_Rich · May 21, 2014 at 03:42 PM

I Think this should work

pragma strict

 var door : Animation;
 var OpenSound : AudioClip;
 var CloseSound : AudioClip;
  
 function OnTriggerEnter(Other : Collider)
 {
     if(Other.gameObject.tag == "Player")
     {
        door.Play("SlidingDoor_Open");
        audio.clip = OpenSound;
        audio.Play();
     }
 }
  
 function OnTriggerExit(Other : Collider)
 {
     if(Other.gameObject.tag == "Player")
     {
        door.Play("SlidingDoor_Close");
        audio.clip = CloseSound;
        audio.Play();
     }
 } 
Comment

People who like this

0 Show 0 · 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

Answer by TristoWestside · Nov 06, 2016 at 02:52 PM

can someone please repost this in c#? im sure im just making one simple mistake. but when i go back to say "doorClose" is quickly loops between say " close and open". ?

btw i did not get the code from this question. this is my code here.

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public Animator anim;

 public float speed;
 public float idleSpeed;

 void Awake()
 {
     anim = GetComponent <Animator> ();

     speed = 5f;
     idleSpeed = 0f;
 }

 void Update () 
 {
     if (Input.GetMouseButton (0)) 
     {
         transform.Translate (Vector3.forward * idleSpeed * Time.deltaTime);
         anim.SetBool ("Idle", true);
     } 
     else 
     {
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
         anim.SetBool ("Walk", true);
     }
 }

}

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

Animation with frozen player ?? 0 Answers

How to handle animation of multiple Coins? 1 Answer

How to make my animated axe collide with tree properly?? 0 Answers

Trying to play other objects' sound and animations from Raycast hit 1 Answer

Possible to stop collider from "popping" through plane when re-centering? 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