Unity 2D game Shooting Target Problems - unity3d

so basically i have a target like the type for archery the 3 rings (bullseye, inner circle and outer circle)
now i basically used a cylinder to create these and then added to them rigidbody2D and a circleCollider 2D , now my problem is because the rings are essentially on top of each other i have them layered out on the z axis a little to make them all visible but when it comes to doing a raycast2d on the target it isnt picking correct ones up for example it goes from outer circle straight to bullseye and skips out inner circle yet all have colliders set up the same way
i cant figure out a way to overcome this and if not ill have to change to a different target where nothing overlaps in order to get it to work but i would really like the archery type targets
Thanks

You could just vary the distance from the camera for each ring so that the ones over the top are hit first.
Alternatively you could add tags to the three rings, use raycast all, and check the tags of all hit colliders to decide which one was hit first. For example, if all 3 register a hit, then you know the center was hit, and if the outer 2 register then you know it's the inner ring, and so on.
http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

Related

Precision in onCollisionStart in flame with flutter

I'm attempting to write a bouncing ball game using flame in flutter. To detect collisions the onCollision and onCollisionStart methods are provided. What I had hoped is that onCollisionStart would give a precise location when two objects first hit each other. However, instead it gives a list of positions indicating where the two objects overlap after the first game-tick when this happens (i.e. onCollisionStart is called at the same time as onCollision, but is not called a second time if the same two objects are still colliding on the next tick).
This is illustrated in the attached picture. The collision points are marked with red dots. If the ball were moving downwards, then the ball would have hit the top of the rectangle and so should bounce upwards. However, if the ball were moving horizontally, then its first point of contact would have been the top left corner of the box, and the ball would bounce upwards and to the left.
If I want to work out correct angle that the ball should fly off, then I would need to do some clever calculations to work out the point that the ball first started hitting the other object (those calculations would depend on the precise shape of the other object). Is there some way to work out the point at which the two objects first started colliding? Thanks
What you usually need for this is the normal of the collision, but unfortunately we don't have that for the collision detection system yet.
We do have it in the raytracing system though, so what you could do is send out a ray and see how it will bounce and then just bounce the ball in the same way.
If you don't want to use raytracing I suggest that you calculate the direction of the ball, which you might already have, but if you don't you can just store the last position and subtract it from the current position.
After that you need to find the normals of the edges where the intersection points are.
Let's say the ball direction vector is v, and the two normal vectors are n1 and n2.
Calculate the dot product (this is build in to the vector_math library) of the ball direction vector and each of the normal vectors:
dot1 = v.dot(n1)
dot2 = v.dot(n2)
Compare the results of the dot products:
If dot1 > 0, n1 is facing the ball.
If dot2 > 0, n2 is facing the ball.
After that you can use v.reflect(nx) to get the direction where your ball should be going (where nx is the normal facing the ball).
Hopefully we'll have this built-in to Flame soon!

Fastest way to determine line of sight on a grid

I am improving a video game of mine where I currently have the problem that NPCs can shoot through walls. In order to fix this issue, I have decided to completely re-do my work regarding ground combat.
I use a tight grid for finding paths from any point A to B. I store my grid as an image-like structure and use Jump Point Search for the pathfinding itself. My goal is to find a location that is as far away from the enemy as possible (limited by the firearms's range) that also has a direct line of sight to the enemy.
In my latest approach, I determined all nodes in my grid that are within weapon range and that are connected to the enemy's location (colored in green in the image below). The final missing part is filtering these points so that only the points with a valid line of sight remain.
I can also detect the border lines (colored in blue) very fast. I could check for every node, if a line from this node to the enemy would intersect any border line. If that is not the case, I have clear line of sight. However, I assume this to be neither fast nor the optimal solution.
Do you have any ideas or suggestions? I am glad for any hint!
I visualized the grid in the following image to give you an idea of what I am talking about. If a character would stand in the lower right green area, he could not shoot the enemy in the upper left area because of missing line of sight.

Unity 3D object with Rigidbody sliding

I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!
Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!
Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.
The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.

I want to check if a line between two vector points falls into a particular area or a set of vector coordinates?

Note : I am coding in Unity, using C# script. I cannot use trigger hit detection using Raycasting because there are a lot of trigger colliders in between the target and source which detect touches and the sort. So the ray hits the other triggers before even reaching it's target, which is not desirable.
What I want to accomplish basically is return a boolean value if a vector line crosses or intersects a particular set of vector coordinates or an area.
For example: Detecting a laser entering into a fog in between it's path when shooting at it's target. The fog is a trigger collider based game object.
Edit: Another example would be to check if a line crosses a 2D box area in a 2D graph. Keep in mind, I cannot use collision detection or Raycast hit here.
There is no need for code, just explain the concept of how it could be accomplished. Though a code snippet is also welcome. Thankyou!
[...] So the ray hits the other triggers before even reaching it's target, which is not desirable.
What about putting that on Layers? You can specify LayerMasks for Raycasts.
You could consider the edges of the 2D box as lines i.e 4 lines and check for intersection of a line with these 4 edges using line to line intersection technique. If the line however is small enough to fit inside the box, and you want to treat it as a valid intersection, then check if the two points of the line is inside the bounds of the box.

Preventing an object from landing on its side; ensuring it always lands face-up or face-down

Currently I am doing a project of shell toss in Unity. The shell is made in Blender and imported in Unity. I attached a single box collider to the shell.
Now when I apply the random force to the shell to flip it, it sometimes rests in the vertical up position when either a result of heads (face-up) or tails (face-down) should have occurred.
I have tried changing the axis, increasing the gravity. But none of these solutions worked.
What I have done for now is when the shell is in a resting position after it lands, I rotate the shell 90 degrees - but this is not a great solution, since it is slow and takes time to check whether the shell has come to rest or not.
I am looking for the better idea so that the shell should only rests in a heads or tails state.
If you use a box collider for your shells, you are going to end up in this problematic situation where the flipped shell sometimes ends up sitting sideways, instead of face-up or face-down.
In this situation, I would recommend making use of Capsule Colliders, which are cylindrical colliders with a rounded top and bottom. If a capsule collider lands on either end, it'll fall over sideways:
However, one capsule collider isn't enough - otherwise, the shell will start rolling around after it falls over. I suggest 2 or 3, oriented in a cross such that their tips correspond to the sides of the shell:
Now, if the shell lands on any of its sides, it will topple over onto one of the faces of the cross - either face-up, or face-down. To add capsule colliders to your GameObjects, just click on Add Component, and select Physics > Capsule Collider. Then modify the properties to get them into the desired positions:
The Direction you choose should probably be X-Axis and Z-Axis. Once you have the correct orientation, change the Center value to move the colliders to the right positions to make a cross through the shell. Then, alter the Radius of the colliders to affect the thickness of the cross, and the Height so their tips line up with the edges of the shell.
Hope this helps! Let me know if you have any questions.