I am trying to get the camera to move when I collide with another Sprite, but when I collide, OnTriggerEnter2D is not being called, someone help me. Both of the sprites are set as Triggers. Here is the script associated with the problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed;
private Camera playerCamera;
public GameObject door;
private GameObject player;
void Start () {
player = this.gameObject;
playerCamera = Camera.main;
}
void Update () {
//moving the player
if (Input.GetKey(KeyCode.S)) {
player.transform.position += Vector3.down * Time.deltaTime * speed;
}if (Input.GetKey(KeyCode.W)){
player.transform.position += Vector3.up * Time.deltaTime * speed;
}if (Input.GetKey(KeyCode.A)) {
player.transform.position += Vector3.left * Time.deltaTime * speed;
}if (Input.GetKey(KeyCode.D)) {
player.transform.position += Vector3.right * Time.deltaTime * speed;
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
print("Testing to see if hit door");
if (collision.gameObject.CompareTag("right"))
{
for (Vector3 camPrevPos = playerCamera.transform.position; playerCamera.transform.position.x < camPrevPos.x + 22.7; playerCamera.transform.position += (Vector3.right * Time.deltaTime))
{
playerCamera.transform.position += Vector3.right * Time.deltaTime;
}
print("has hit a door");
}
}
//senses if player has hit a door
public void MoveCamera(Collision2D collision){
print("Testing to see if hit door");
if (collision.gameObject.CompareTag("right")) {
for (Vector3 camPrevPos = playerCamera.transform.position;playerCamera.transform.position.x < camPrevPos.x + 22.7;playerCamera.transform.position+=(Vector3.right * Time.deltaTime))
{ playerCamera.transform.position += Vector3.right * Time.deltaTime;
}
print("has hit a door");
}
}
}
Let me know of you need more info!