I've got a ray cast being created from the 'yellow X' in the pictures below. It is detecting the collision hit, however the distance is way off (~ 21 instead of 7 etc) . The green X shows where the collision is taking place and hence how long the distance should be
The Line Renderer is being drawn from start point along the forward vector for the ray hit distance, so in the second picture you can see where I have a much smaller plane with better tessellation it works fine, but with a larger ground plane in the first picture it is way off. It also works fine with other objects in the scene like small boxes.
I have tried using a box collider which still has the same problem.
Any ideas?
Related
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!
I am trying to get a stretched out cube (which we can call a plane for the sake of discussion) to orient itself to the normal vector of a plane described by three points. I wrote a script to find the normal of three points, and then used transform.LookAt to have the planes align. However, I am finding that this script is not working at all how it is intended to and despite my best efforts I can not figure out why.
drastic movements of the individual points hardly effect the planes rotation.
the rotation of the object when using the existing points in the script should be 0,0,0 in the inspector. However, it is always off by a few degrees and as i said does not align itself when I move the points around.
This is the script. I can also post photos showing the behavior or share a small unity package
First of all Transform.LookAt takes a position as parameter, not a direction!
And then it
Rotates the transform so the forward vector points at worldPosition.
Doesn't sound like what you are trying to achieve.
If you want your object to look with its forward vector in the given normal direction (assuming you are calculating the normal correctly) then you could rather use Quaternion.LookRotation
transform.rotation = Quaternion.LookRotation(doNormal(cpit, cmit, ctht);
alternatively to this you can also simply assign the according vector directly like e.g.
transform.forward = doNormal(cpit, cmit, ctht);
or
transform.up = doNormal(cpit, cmit, ctht);
depending on your needs
I am getting started with Unity and am just trying to get my head around the units. What are these units? It seems they are their own 'quantity' and to treat 2 units as 2 times the value of 1 unit.
Anyway - I am trying to workout how to optimally calculate transforms to objects sit exactly where I want them to.
In my scene I have a terrain and a cylinder as so:
As you can see my cylinder is floating. I want the cylinder to sit perfectly on top of the terrain.
My terrain is at the following transform: 0,0,0 and scale 0,0,0 (not sure how to tell it's dimensions yet).
My cylinder is part of a new object, as so:
My FirstPersonPlayer is at transform: 85.9,2.165,51.8 and scale 1,1,1. My Cylinder is at 'localposition' 0,0,0 and local scale 1.2,1.8,1.2
Now - the transform of FirstPersonPlayer on the y axis appears to be what I need to correct.
Currently it is set to 2.165 and is floating a bit above the terrain.
Through manually shifting it, around 1.85 looks about right - but I want to know how to calculate that, rather than doing a finger in the air 'that looks about right'.
Can anyone help me? (Before you suggest using gravity etc , I actually am, but don't want the player falling as soon as they start, however slight that may look or feel.
Many thanks,
As per #Nikola Dimitroff the answer is:
You don't have to compute anything, hold Shift + Control and drag the object. Every game engine ever made calls this "Snap to Ground"
I appreciate and agree with the other comments.
I need a curve cylinder collider for my curve water pipe model so that my player red cube can pass easily through.
I tried mesh collider it doesn't works and also tried another trick where I build 2 models in blender one for mesh render, one for collider. one as a whole one in 9 chunks cause mesh collider was not working on whole one object check pic no:1 and check pic no:5 where after exporting both models to unity I applied mesh collider on all 9 chunks of pipe separately to get perfect results in pic no:5 results of collider look's perfect but not working perfect the red box Player can't pass through the pipe
Collider doesn't let the player get inside and out from another side as shown in pictures. I need a perfect collider for my curve pipe so that my player can easily pass through. In pic one I showed that after applying mesh collider results. In pic two I showed I build my model again but in 9 chunks as one chunk is selected you can see, To apply mesh collider on all 9 chunks separately to get good results. In pic 5 I showed final result it looks good but not working the collider is not letting my small red box player inside the pipe and pass through it. In pic 3 I am aiming. In pic 4 I fire and showed that our player does'nt get in the pipe.
This problem is caused because Unity is turning your Mesh into a convex shape, which removes holes from the tube. It will also reduces the vertex count down to 255 triangles, the maximum for a convex mesh. That is why the collider gets so deformed in the first picture.
There are 2 main options for you here, because tubes are inherently non-convex shapes.
Make sure Convex is not checked on the mesh collider and that there is no rigidbody on the tube. Then, the cubes should be able to enter the tube. However, this will mean that other MeshColliders will not be able to collide with the tube. If you're only colliding it with BoxColliders, SphereColliders, or CapsuleColliders and so on, this should work fine.
Break up the tubes into a series of convex shapes, and make each one a separate MeshCollider. Then the situation you have here should work fine.
From the documentation of MeshCollider:
Convex - Tick the checkbox to enable Convex. If enabled, this Mesh Collider collides with other Mesh Colliders. **Convex Mesh Colliders are limited to 255 triangles. **
...
Mesh Colliders that do not have Convex enabled are only supported on GameObjects without a Rigidbody
component.
I'm currently trying to develop an arkanoid-like game in Unity but I'm having a problem that I can't seem to be able to solve with a satisfying solution.
When the ball hits the edges of two adjacent bricks, it "reflects" the velocity in an unexpected way: the ball goes to the direction where it was coming from.
For information, I have a world space size of : 5.625x, and each brick has a size of 0.703125 for 8 bricks per line. Also, every bricks have the same Y size for the collider.
I tried increasing the x size of the collider to some reasonable extent (0.74x for example) to make sure there's no gap between two bricks but the problem still occurs sometimes.
Here is a very ugly picture illustrating what's happening (the green arrow being the expected trajectory):
Thanks in advance for your help!
EDIT: Here are the inspector informations regarding the ball and the bricks. Regarding the lines of bricks, it's basically eight game objects with a boxcollider2d and a kinematic rigidbody2d.
The ball material has a friction of 0 and a bounciness of 1.