- Home /
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.
string direction??
You need to give some explanation of how your code works or not and errors encountered.
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)?
"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.
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.
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.
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!
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.
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
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
Follow this Question
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