1
\$\begingroup\$

The player in my game can teleport like Noctis from ffxv.The player shots a weapon projectile and then teleports to the projectile. I'm using raycast to calculate the weapon position after thrown. However, I need to check if the weapon is stuck in an unteleportable position after being thrown. It is possible to have multiple penetrating colliders. Ex: spear gets sandwiched between two walls that is narrow enough for the weapon to pass through but not tall enough for the player to stand. I want to somehow check for a nearby valid position for after teleporting in that situation.

enter image description here

I also want the teleport to fail if the weapon is horizontally in the middle of the "pipe".

I could do a bunch of overlapBox checks within a radius distance from the teleport point but that doesn't seem efficient. I want to give the player some leeway when aiming a teleport.

Also, the teleport is not immediate. The player can wait some time before teleporting to the weapon. So it is possible for a moving platform to block a raycast from the player. In which case I still want the raycast to succeed.

I'm using unity and a custom raycast based 2d character controller for normal movement.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I suspect raycast may not be the best option. Rather, actually shooting something that represents the teleport location might work better.

In the event you want to stick with raycasts, a standard one is "eaten up" by its first collision, but there are modified ones that don't. If I recall it's something like RaycastAll.

The advantage of shooting the actual teleport location along with the weapon is then it follows your weapon's physics or custom behaviour if the weapon bounces or is blocked by something.

The moment the player clicks "teleport" You then take this location, and search around it for a valid spot. There are many ways to do this, the simplest of which is to make a 2d mesh of valid spots of the ground that can be teleported to, similar to a navmesh. Then you find the nearest spot on this mesh from the teleport location, and if it's farther than some threshold you disallow the teleport, if it's allowed, you let the teleport happen to this valid location.

I can forsee cases where you want them to be able to teleport in ways that can kill themselves, you you might make this (invisible) teleport mesh extend across pits etc, meaning that you can teleport there, but then you'll fall to your death.

Hope that gives you some ideas to get started.

\$\endgroup\$
1
  • \$\begingroup\$ The game is 2D. I am not using tilemaps so the solution slipped my mind. All I'd need to do is locate the teleport tile and look for free tiles assuming the player can fit inside a single tile. Thanks. \$\endgroup\$ Apr 7, 2020 at 3:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .