So maybe I’m just not seeing the forest for the trees, but I can’t find a suitable solution to the following problem. It’s about an “idle game” (not quite idle, but idle components). In it, for example, one spaceship shoots at another. But since the pilot of the ship has skills (e.g. for targeting), he shoots automatically and the better this skill is, the better he should hit (solved). So now, in the case that I hit by chance and skill, I use a ray cast and, for example, a line renderer for the laser shot. It just looks pretty boring if he always hits the enemy in the same place or places (the ships also consist of sub-objects, so I’ve solved it so that in the event of a hit, a random child game object is selected as the target. Of course, you could do a lot more with it (empty game objects) to achieve more individual hit points.
But is there perhaps a way to find a random point on the game object (including its sub-objects, that would be great, but if not, I can solve that) and thus make the hit pattern even more random? For example, using a random point on the texture that is not transparent???
GameObject’s don’t inherently have any volume to them.
So really this is a question of finding a random point on the surface of whatever visual aspect you have attached to your GameObject.
Is this a Sprite?
Is this a MeshRenderer?
Is it something else?
From there I could think of various things. Lets say you could use the ‘bounds’ of your renderer if the object fits tightly into its bounds. There you’re just getting a random point on the face of the bounds facing your “laser”.
If it’s not like that and instead a more organic shaped mesh like a space ship with a mesh collider. You could instead get a random point on the bounds (the same face as the bounds above, or maybe on some spherical bounds you might have calculated) and then ray cast from that point towards the center of the bounds and take the point that got hit on the collider.
Hihi yes, sorry, I always think in 3D, I don’t like 2D games (that’s just how I am…). So
The parent game object itself only holds a script which ultimately controls the ship (movements, taking damage, destruction…).
Its children are 3D objects which all have a collider, but mostly a box collider (95%), so they are not overly precise. The children also have the materials and textures. Children are also particle systems for the engines and a camera for a render texture (enemy vision, so to speak).
As I said, I use a script to determine the probability of a hit (depending on the opponent’s skill and distance) and then use a random number to determine whether it hits or not.
Currently it is like this: If it hits, the parent object in the script has an array of all child objects that are spaceships (e.g. cockpit, engine, wings…) and I select a random object, then set a raycast in its direction and then
a) a line renderer between the weapon output and the child’s game object
b) a hit effect (partial system) on the hit of the raycast
c) sound effects, text with damage…
but even with 20 child objects, it’s always the same points that are hit. Not everyone will notice that, but I don’t think it’s nice. And I also don’t think it’s a good solution to just attach another 50 empty game objects to it…
It would be nicer if it found the parent, then automatically looked for a 3D point on one of the children and shot there (so that’s where the line renderer and the hit effect go).
A somewhat more bruteforced idea: Get the objects AABB and compute a random point in that.
Then raycast. If nothing is hit, take another random point; repeat until a hit which you then use for the effect the player actually sees.
Instead of using the pilot’s skill to predetermine whether your pilot hits and then picking a random point on the enemy ship to hit, you should just do a raycast towards the enemy ship with some skill derived randomness added to the raycast direction. If the raycast hits the enemy then do damage…