Unity se Asigna a un GameObjects con un propósito, puede ser para muchas cosas, para perder vida, ganar vida, eliminar al jugador, cambiar de escenario , activar un mensaje ECT es una manera de identificar a un objeto para un propósito
Tenemos nuestro Avión que es el que esta abajo y luego tenemos otro Avión Negro arriba a la derecha
Aquí tenemos a nuestro Avión con su Box Colíder. el Istrigger vemos que lo tiene activado. También arriba tiene un Tag : Player
un Rigibody 2D
y un Script que ahora pondré aquí
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plane : MonoBehaviour
{
public float velocidad;
public float rotacion;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
float h = -Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 speed = transform.up * (v * velocidad);
rb.AddForce(speed);
float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));
if (direction >= 0.0f)
{
rb.rotation += h * rotacion * (rb.velocity.magnitude / 5.0f);
}
else
{
rb.rotation -= h * rotacion * (rb.velocity.magnitude / 5.0f);
}
Vector2 forward = new Vector2(0.0f, 0.5f);
float steeringRightAngle;
if (rb.angularVelocity > 0)
{
steeringRightAngle = -90;
}
else
{
steeringRightAngle = 90;
}
Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;
Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.blue);
float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));
Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);
Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.black);
rb.AddForce(rb.GetRelativeVector(relativeForce));
}
y Aquí tendríamos nuestro script del movimiento
Ahora a este Script abajo le vamos a poner lo siguiente
El enemy tiene un tag un box collíder2D con el Isttrigger activado y un rigibody2D y cada vez que choque sobre el nos mandara una mensaje en la consola
Ahora queremos que cuando choque cambiemos de color pues pondremos lo siguiente
private void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == «chocar»)
{
gameObject.GetComponent().material.color = Color.blue;
Debug.Log(«Cambiar de color»);
}
Con este código cambiaremos de color