- Home /
OnTriggerEnter2D problem
Ok, so I want the camera to move when the player touches the trigger, (the trigger had "trigger" checked in the editor) but for some reason its not working, any advice would be greatly appreciated!
using UnityEngine; using System.Collections;
public class OnYCam : MonoBehaviour { public GameObject Player; public GameObject Trigger; public bool TriggerEntered; public Transform[] target; public float speed;
private int current;
void Update()
{
if (TriggerEntered == true)
{
Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
GetComponent<Rigidbody2D>().MovePosition(pos);
}
else current = (current + 1) % target.Length;
}
void OnTriggerEnter2D(Collider2D col)
{
if (Player == Trigger)
{
TriggerEntered = (true);
}
}
}
$$anonymous$$ake sure your character has a collider and a rigidbody (in this case a rigidbody2D). $$anonymous$$ost people forget to add the rigidbody, but it is required to send a collision event.
Answer by Nivbot · Nov 09, 2018 at 03:34 PM
void OnTriggerEnter2D(Collider2D col) { if(col.CompareTag("Player")) { triggerEntered = true; } }
you could also use col.name and compare the name of the tranform, or col.tag, but CompareTag is generally thought to be faster.
Your answer
Follow this Question
Related Questions
Problem comparing contents of arrays 1 Answer
How do i make two if statements 3 Answers
Why is this simple C# script not working? 3 Answers
Trigger to play music needs to check if the same music is already playing 1 Answer
Enum Type Inventory? 1 Answer