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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by buttholejohnson · Nov 20, 2014 at 04:20 AM · animatorcolliderstriggers

Insane problem with triggers

I think I might have worded this terribly wrong the first time I tried to ask so I'm trying again.. Ok, so we have 3 objects- Player, P3Trig, and Platform. Objective: When Player enters P3Trig, Platform begins it's animation. The script for this MUST be attached to the Platform object.

My guess is something similar to this:

 using UnityEngine;
 using System.Collections;
 public class Platform : MonoBehaviour {
     public GameObject Player;
     public GameObject P3Trig;
     Animator animator;
     bool moveStart;
     void Start ()
     {
         moveStart = false;
         animator = GetComponent<Animator>();
     }
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.tag == "Player" && Collider2D.tag == "P3Trig")
         {
             Debug.Log("Player has contacted the trigger");
             moveStart = true;
             Pmove3Control ("Start");
         }
     }
     void Pmove3Control(string direction)
     {
         animator.SetTrigger(direction);
    

 

That is all. Presumably a fairly simple task gone wrong in some simple way, but more than a day has now been spent attempting a solution. Are there other better resources then the unity manual because that thing is getting me nowhere.. Please please help.

Comment
Add comment · Show 4
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 meat5000 ♦ · Nov 20, 2014 at 04:23 AM 0
Share

string direction??

You need to give some explanation of how your code works or not and errors encountered.

avatar image Uldeim · Nov 20, 2014 at 04:25 AM 0
Share

You say the script $$anonymous$$UST (emphasis yours) be attached to the Platform object, but what's the reason for that? OnTriggerEnter2D only fires on its own triggers, AFAI$$anonymous$$, so you'll need to have that on either Player or P3Trig. Could you do that, and then make Pmove3Control public and call it (if that's what needs to be on Platform)?

avatar image tanoshimi · Nov 20, 2014 at 08:05 AM 1
Share

"When Player enters P3Trig, Platform begins it's animation. The script for this $$anonymous$$UST be attached to the Platform object.". Impossible. Collision messages are sent only to the objects involved in the collision. You can't have a script on Object C that listens for collisions between objects A and B.

Solution is simple - put this code on Player or P3Trig ins$$anonymous$$d.

avatar image meat5000 ♦ · Nov 20, 2014 at 01:07 PM 0
Share

You can Detect trigger with P3Trig but you must then use a routine on that object (Probably via GetComponent or Send$$anonymous$$essage) to fire the routine on the Platform which makes it move.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Shawn Miller · Nov 20, 2014 at 08:35 AM

As others have mentioned, collision is only reported between the 2 objects that touch, even in the case where multiple overlaps occur. If these are 3 completely separate objects which need to remain separate, then when Player and P3Trig touch, both will receive that event, but not Platform. If this script needs to be on the platform, then you will need a separate script that notifies Platform when the player enters P3Trig through some function.

 public class PlatformNotifier : MonoBehaviour
 {
   public Platform platform;
 
   void OnTriggerEnter2D(Collider2D col)
   {
     if(col.tag == "Player")
     {
       platform.PlayerEnteredTrigger();
     }
   }
 }
 
 public class Platform : MonoBehaviour
 {
   public void PlayerEnteredTrigger()
   {
     Pmove3Control("Start");
   }
 }

You would then put the PlatformNotifier script on P3Trig and drag the Platform object onto the platform variable in the inspector.

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 buttholejohnson · Nov 20, 2014 at 12:51 PM 0
Share

Well I super duper appreciate the help. I'm up at 5 not able to sleep thinking about this solution and am probably going to have to spend awhile to get it working, but I will let you know. As it stands currently though, I applied the above script to P3Trig, but for some reason the public platform variable refuses to appear in the inspector, so the new message is:

  'the name P$$anonymous$$ove3Control does not exist in the current context"

Which makes me think that this is the most accurate response: ""When Player enters P3Trig, Platform begins it's animation. The script for this $$anonymous$$UST be attached to the Platform object.". Impossible. Collision messages are sent only to the objects involved in the collision. You can't have a script on Object C that listens for collisions between objects A and B.

Solution is simple - put this code on Player or P3Trig ins$$anonymous$$d."

That makes sense. The reason why I wanted them separate is because when learning how to move a platform side to side, I found that using the animator was by far the least intensive and most visual way to accomplish the task. The only problem was that when using this method in conjunction with a trigger it requires the object being animated to be a child of it's trigger. That would be fine but it makes it so any future adjustments of the platform also drag it's associated trigger around the scene as well, making things generally messy. I thought this would be a way around that, but now I know to start from the bottom up ins$$anonymous$$d, which is probably better. Thanks a million for your time!

avatar image Shawn Miller · Nov 20, 2014 at 08:09 PM 0
Share

If the platform variable isn't showing up then you could always switch it to a GameObject and then switch the collision up to:

 void OnTriggerEnter2D(Collider2D col)
    {
      if(col.tag == "Player")
      {
        Platform platformScript = platform.GetComponent<Platform>();
        platformScript.PlayerEnteredTrigger();
      }
    }
 }

I didn't encounter any issues when testing it so the only other thing I can think of is that the scene is set up in a way other than how I'm envisioning it.

avatar image
0
Wiki

Answer by buttholejohnson · Nov 20, 2014 at 08:44 PM

Well I super duper appreciate the help. I'm up at 5 not able to sleep thinking about this solution and am probably going to have to spend awhile to get it working, but I will let you know. As it stands currently though, I applied the above script to P3Trig, but for some reason the public platform variable refuses to appear in the inspector, so the new message is:

  'the name PMove3Control does not exist in the current context"

Which makes me think that this is the most accurate response: ""When Player enters P3Trig, Platform begins it's animation. The script for this MUST be attached to the Platform object.". Impossible. Collision messages are sent only to the objects involved in the collision. You can't have a script on Object C that listens for collisions between objects A and B.

Solution is simple - put this code on Player or P3Trig instead."

That makes sense. The reason why I wanted them separate is because when learning how to move a platform side to side, I found that using the animator was by far the least intensive and most visual way to accomplish the task. The only problem was that when using this method in conjunction with a trigger it requires the object being animated to be a child of it's trigger. That would be fine but it makes it so any future adjustments of the platform also drag it's associated trigger around the scene as well, making things generally messy. I thought this would be a way around that, but now I know to start from the bottom up instead, which is probably better. Thanks a million for your time!

See

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 buttholejohnson · Nov 20, 2014 at 08:47 PM 0
Share

Perfect. Got it. Entirely possible.

  using UnityEngine;
     using System.Collections;
     
     public class bla : $$anonymous$$onoBehaviour {
     
         public Animator leAnimator;
         void OnTriggerEnter2D(Collider2D col)
         {
             if(col.tag == "Player")
             {
                 Debug.Log ("derp");
                 leAnimator.SetTrigger("Start");
             
             }
         }
     }

The code goes on the trigger, the platform animator gets dragged in on the inspector, and the player can trigger it while still being a separate object. Cool.

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

29 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

Related Questions

Trigger with get key down problems 0 Answers

Use Of OnCollisionStay()? 2 Answers

how to get current gameObject animator triggers? 1 Answer

IsTouching and IsTouchingLayers for Colliders are not defined? 1 Answer

Trigger not playing animation 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