Question by
uleutz123 · Apr 26, 2019 at 06:43 AM ·
collision2d game2d-platformerhowteleporting
How to check for colliders when teleporting the player in a 2D game?
Hello there!
I'm trying to make my player teleports when hitting the F key but i don't know how to check if there are any collisions on his way. The result is that the player teleports out of the world map and falls down into abysm
How can i make him not going through colliders?
Here's what i did:
if (Input.GetAxis("Horizontal") < 0 && Input.GetKeyDown(KeyCode.F))
{
transform.position = new Vector2(rb.position.x - teleportDistance, rb.position.y);
}
if (Input.GetAxis("Horizontal") > 0 && Input.GetKeyDown(KeyCode.F))
{
transform.position = new Vector2(rb.position.x + teleportDistance, rb.position.y);
}
There's a variable 'teleportDistance' and its set on the Player main script.
Comment
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object 0 Answers
Player is bouncing when colliding with wall object 0 Answers
How can I fix this with my ground check? 1 Answer
Error: An object reference is required to access non-static member, help? 2 Answers
Player passenger moving when being pushed by two platforms 0 Answers