- Home /
Check if player object is right above this object
Hey Unitarians,
What I have is a player, and what I want is that when the player moves over a specific tile, the tile detects that the player is right above it, and freezes the player in the position until the user sets him free.
I've got the freezing all set up, but what I can't figure out yet is how exactly I can have the tile detect the player right above it. The player moves in the x and y axises. The z-axis remains constant for all objects in the scene. This is the code I have so far:
if(transform.position.x == player.transform.position.x &&
transform.position.y == player.transform.position.y - 1)
{
Debug.Log ("Player is above me");
// player is directly above this tile
pausePlayer = true;
}
I'm thinking of using raycasts, but I have absolutely NO idea how to use those things at all. If you are using raycasts, it'd be great if you could write some code so I can understand. If you are writing code, it'd be preferred if it was in C# but I'm sure once I get the concept I can translate from UnityScript if needed.
Also note: Having a trigger collision will NOT work. I've tried, but it simply does not work with the gameplay.
Thanks in advance! YomanAwe
Raycasting is a viable answer, but there may be simpler solutions. Is this a 2D or 3D game? Are you using an Orthographic or a Perspective camera? Do all tiles freeze the player or just some tiles?
It's technically a 3D game, but played as a 2D game. It is a perspective camera and only some tiles freeze the character
Answer by FirePlantGames · Jul 16, 2014 at 01:29 AM
make sure that the X position actually equals the EXACT same X position of player object, here is a ray cast code:
var hit : RaycastHit;
if (Physics.Raycast(transform.position,Vector3.down,hit))
{
if (hit.gameObject == player.gameObject) //the players .gameObject is there because i'm not sure if you have it set to a transform, if it's a GameObject then you can be rid of it :)
{
Debug.Log ("Player is above me");
// player is directly above this tile
pausePlayer = true;
}
}
that should work, hope I helped :D be sure to tell me how it goes!
@memorymod - A couple of suggestions if the OP ever answers my comment above. First since since the player moves on the XY plane, I suspect the tiles will be either forward or back of the player, not above or below it. That means the raycast will use either Vector3.forward, or Vector3.back. Second, it is far more efficient for the player to raycast against the tiles and detect a property of the tiles and do the freeze rather than have all the tiles doing the raycast and having to inform the player he should freeze.
@robertbu what should I do then? Since all the tiles the player interacts with are below the player, what should I do so the player canact based on the tile that is below it?
Are you using 2D or 3D colliders on your objects? Is 'below' lower on the 'Y' axis or is it behind as in further out the 'Z' axis?
3d colliders. everything in my game is 3d. below is lower on the y axis. the z axis is always constant for everything in the game. The player has a box collider and the width of the box collider is exactly the same width of the pause tile. shouldn't be hard but I have NO idea how to do it