- Home /
How to detect that an object (ball or disk) stays in between the tile and on the border of a tile?
I have a single 3D tile made in maya, and I created a tile surface with code, each tile has its own border , I want to detect that if my player walks over the tiles and if player stops on the tile border it will show me some message or if the player stops in between the tile it will show me another message. Please help me. @hamstar , @Tomer , @Starwalker , @AlienFreak , @Furjoza .
Answer by rocket350 · Jul 23, 2016 at 06:17 PM
One possibility would be to add a collider to every tile when creating them, the collider being smaller than the tile. If the player ends his move outside all of the colliders, you can display a message, as he is no longer directly on any of the tiles
For that you would have to add the following lines to the code where you create each of those tiles at runtime:
int x; // Play around with these values so that
int y; // while the player is still in an acceptable
int z; // position on the tile, he touches a collider with these dimensions
//The next lines needs to be repeated on every created tile
//so be sure to place it in a foreach loop or something similar
Collider tempCollider = tempTile.AddComponent<Collider>();
tempCollider.isTrigger = true;
tempCollider.x = x;
tempCollider.y = y;
tempCollider.z = z;
tempTile.AddComponent<BoundaryChecker>();
Here is the code for the BoundaryChecker.cs class:
using UnityEngine;
using System.Collections.Generic;
public class BoundaryChecker: MonoBehaviour {
private static List<BoundaryChecker> touchingColliders;
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "Player")
touchingColliders.Add(this);
}
void OnTriggerExit(Collider other) {
if(other.gameObject.tag == "Player")
touchingColliders.Remove(this);
}
public static bool isPlayerOnTiles() {
if (touchingColliders.Count == 0)
return false;
else
return true;
}
}
When the player ends his turn, you can call the static function BoundaryChecker.isPlayerOnTiles() which returns a boolean depending on whether the player is touching any of the colliders or not, and display a message accordingly.
Note:
Your player GameObject must have the tag "Player".
Your answer
Follow this Question
Related Questions
How to detect the distance between two edges (2D sprites) 1 Answer
C# How to Detect Edges of a Collider 0 Answers
Detect object borders to add collider 0 Answers
Gothic-like climbing and edge detection 0 Answers
How to Get Image edge size 2D?? 1 Answer