Ways to Unity 8: Know what you are collided with
Purpose
If we are the protagonist, we’ll meet a lot of people(No, objects).
We or the Game need to react in different ways to adapt to the collisions we meet.
How do we do it
We tag different objects.
Tag is a fancy word for naming.
From the Inspecor
of an object. You should see the Tag
options that you need.
The script of our player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private bool isOnGround = true;
private bool isGaveOver = false;
void Start()
{
}
void Update()
{
}
private void OnCollisionEnter(Collision other) {
if (other.gameObject.CompareTag("Ground")) {
isOnGround = true;
} else if (other.gameObject.CompareTag("Obstacle")) {
isGaveOver = true;
}
}
}
The author
yingshaoxo