- Home /
Question by
EpicGamer545 · Dec 01, 2021 at 10:14 AM ·
transform2d-platformertransform.positiontilemapteleporting
How to add a teleportation mechanic like ender pearls in Minecraft
I am making a 2D platformer in Unity 2020.3.23f1. I have made a shooting mechanism and have written a script to save the location of where it lands. But when I added a script to change the players position to the position where the projectile lands, I sometimes teleport below ground.
How I want the teleportation mechanic to work - If I throw it at the top surface of a tilemap, I teleport there. If I throw it at a wall, I teleport to the side of the wall. If I throw it at the ceiling, I teleport to the ceiling.
How to do this? I have been searching for an answer for days.
Here is the teleportation script (inside the player) -
private void Update()
{
if (shouldTel == true)
{
transform.position = new Vector2(telPoint.transform.position.x, telPoint.transform.position.y + 1);
shouldTel = false;
}
}
Here is the other teleportation script (inside the projectile)
private void Update()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, solidChecker);
if (hitInfo.collider != null)
{
if (hitInfo.collider.CompareTag("Ground"))
{
Debug.Log("Teleport");
tel = true;
telPoint.transform.position = transform.position;
}
else
tel = false;
DestroyProjectile();
}
teleport.boolTeleport(tel);
teleport.teleportFunc(hitInfo);
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
Comment