Raycasthit2d




How is Everhood going

Raycasthit2d

RaycastHit2D Question / Need help. Hey guys, I've been playing around with a 2d platformer game for a while now but I'm really struggling to understand how this raycast system works for my enemies to attack my player. Below is my code and ill explain my setup under that. The 3D engine offers Physics.Raycast, which returns true on hit, or false otherwise, and allows you to pass a RaycastHit by reference if you need to know more about the hit. The 2D engine offers Physics2D.Raycast, which instead returns a RaycastHit2D on hit, or null otherwise.

We are doing very good progress on Everhood! Chris is creating more and more in-game content, no one can stop him! From my side (Jordi), I have been working on fixing and creating new in-game logic, don't want to do spoilers but, I bet you will be amazed :D

As I mentioned in Game Localization devlog, trying to not spoil the story and the in-game content is one of our main goals. Here's all we can say for the moment.
To compensate I've made a second mini tutorial/tip for unity c#, you can check the first tutorial/tip here. Gigafast ethernet driver.


Lets create a simple 2D laser! - Unity C#

Difficulty: Beginner

Raycasthit2d example

Lasers..who doesn't like lasers?

As I love both lasers and each of you, well, let's create the laser logic step by step!

Raycasthit2d Array

First, let's create a new empty game object and assign a LineRenderer component to it.


Next, make sure to assign the Sprites-default material and set the alignment to 'Transform Z'


Pycharm professional download. Then, create a new script called Laser and open it.

Now let's get the line renderer component at Start().


Next, we need to know when an object collides with the laser, let's use Raycast!


Finally, we need to set line renderer position according to the raycast. Keep in mind that the line renderer have two points 0 and 1, so we need to set both the start point and the end point.

The start point is going to be the main transform position,

Raycasthit2d

The end point is going to be the raycast hit point position,

If the raycast don't hit something, just set the end point to an infinite or large value.


Assign Laser script to the empty game object we just created, set editor to play mode et voila!


Raycast2d

Raycasthit2d not working

I hope this mini tutorial was useful!

Hit

// Jordi


Wishlist us here so you don't miss any news: