I’m currently trying my first 2D platformer, and I have a problem.
There is a “Triangle” which should behave like this:
-
If there is the player in front of it, the triangle should move on a straight line toward him
-
if the Triangle touches the player it should kill the player and other things
Now the problem is that the two things comes from two different Colliders, the first is a “ray” that starts from the triangle in front of him, and the second that is actually the hitbox of the triangle. How can I tell in the function to which one of the two collider I’m referring to? As it is now, it seems like they activate at the same time, (if the ray is touched the hitbox of the triangle is also activated and vice versa).
I attach here the code://Colliders
public GameObject RayCollider;
public GameObject TouchCollider;//bool
public bool PlayerInFront;
public bool PlayerIsTouching;void Start()
{
TouchCollider.SetActive(false);
RigidBody = GetComponent();
RayCollider = triangle.transform.Find(“RayCollider”).gameObject;
TouchCollider = triangle.transform.Find(“TouchCollider”).gameObject;
}void Update()
{
if (PlayerInFront)
{
RigidBody.velocity = new Vector2(moveSpeed, 0);
TouchCollider.SetActive(true);
RayCollider.SetActive(false);
}if (PlayerIsTouching) { RigidBody.velocity = new Vector2(0, 0); gameObject.transform.position = StartingPosition; collisionCounter = 0; }
}
void OnTriggerEnter2D(Collider2D Collider )
{if (Collider.tag == "PlayerTag") { if (collisionCounter == 0) { PlayerInFront = true; collisionCounter++; } } if (Collider.tag == "PlayerTag") { if (collisionCounter == 1) { PlayerIsTouching = true; collisionCounter++; } }
}
}
//The Collision Counter thing is for desperetion lol, don’t mind it