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