Using raycasting to measure length it passed through an object

So i want to send out a raycast, which first puts the entry point (which is easy to do with RaycastHit2D.point) into an array but then also puts the exit point of the raycast at into an array, which i then can use to measure the length the raycast has traveled through the object, as shown in the picture bellow.

Ive been trying to do this the last 3 days, but i just cant find anyone who knows how to do it, and i havn’t been able to figure out how to do it either

I want to use this in order to change bullet speed when the bullet is passing through objects, in order to change the damage depending on how many walls the bullet has passed through, so i dont want it to just calculate the total velocity after passing through all walls and then applying it to the damage, because then the damage would be lower if an enemy was hiding infront of a wall. I know there is annother way of doing this by making the bullet into an object, and just shooting it into the direction you want it to go, and lowering the speed if its inside a collider, but ive heard that there are problems with this, like the bullet ignoring thin walls if its traveling too fast, and a little bit worse performance, although if someone knows that this is false, please tell me so that i can use the simpler solution

Also, im using c# and im using unity2D

Ok, consider that you have a bullet, that bullet only stops when the speed reaches or falls below zero. So the given bullet is on a path. We will need a variable to tell us if we are currently inside of something as well as one to handle the impact point of that entry. We use Physics2D.RaycastAll to get what it has hit from the beginning of this frame’s travel to the end. We will also need to do a second Physics2D.RaycastAll from the opposite direction (end to beginning)

Now, with this, we will need to figure out if we are currently in something, if so, where or if we exited that something and the number of passes into and/or through another object. We can tell the distance we traveled in this frame, and the amount of distance within that material. You can also set up density on each material to impact the speed.

In the case of what you are asking, RaycastAll is quick and will tell you your information. This should not impact performance negatively until you have many bullets traveling at the same time.

Thanks for this
Ive managed to get it to read the distance it travels within walls, and ive been able to figure out how i would use this information to deal the correct ammount of damage the players who stand behind walls (by using a for argument, which goes through all enter and exit collisions, and if that collision happens to be through an enemy, deal damage * length the bullet has traveled through all walls before that moment)
Although there is one thing ive gotten stuck on, and that is accesing variables from other gameobjects, when for example getting the material density of a wall, or giving the damage to the enemy who was in the way of the bullet.

This is the furthest ive gotten to finding the script attatched to the enemy hit from the collision variable the raycast gives me

CollisionMaterialData colMatData = (CollisionMaterialData)hitEnterCollisions[i].transform.GetComponent("CollisionMaterialData");

although, is there any better way of doing this rather than having to attatch a script to every object i want to be able to hit named “CollisionMaterialData”

There is no magical property you can get off of the object, unless you use things which are not intended for this use, like tag or something else. You would want to use something that could mean lots of things, so you are kind of stuck with the script is my guess. However, there is an alternative.

CollisionMaterialData colMatData  = hitEnterCollisions[i].gameObject.GetComponent<CollisionMaterialData>();
if(colMatData== null){
colMatData  = CollisionMaterialData.Default(); // this should return a new material
}

In CollisionMaterialData, create a static method which returns a new CollisionMaterialData with whatever default values that you want for general objects. This way, you only apply a CollisionMaterialData to an object that has some property other than a default wall, or something.

Sorry, but i don’t get what you mean with your script, this is basically how i understand the problem

2686106--189914--test.png

The reason why i want to find the script inside the object with a class called MaterialData in it is because otherwise i would have a lot of scripts called the same thing but attatched to different objects, which would be really messy, so do you know how i would find the object with the class inside it called MaterialData and then use it, or is there a better way of doing this?

I forgot to add the “use the default materialDensity and damageable bool if a materialData script isnt found” in the example

Also, i get this error:

“Raycast 2D does not contain the definition for “gameObject””

when i try to use your script, and wouldn’t it be better to just put the default value inside the script that calculates the damage, instead of having it acces a default method?