- Home /
Unity high speed collision detect
Hi all, I move a game object by itween and need detect some collision. But the moving speed so fast , so that I can not detect the collision. Are there any way for this solution (don't change the Fixed timestep). Thanks.
Answer by keyp3r · Dec 01, 2013 at 11:55 PM
Absolutely. This should get you where need to be:
public LayerMask CastMask;
Vector3 oldPos;
void Start(){
oldPos = transform.position;
}
void Update(){
//Move the object forward here...
if(Physics.Linecast(oldPos, transform.position, out Hit, CastMask)){
transform.position = Hit.point;
//I hit something! Put a script call here...
}
oldPos = transform.position;
}
So, what I'm doing there is moving the object forward, then drawing a line from my previous position to where I am currently and if I've found any object in-between we can assume that I should have hit it. Then I store my current position for use on the next frame. Going this route is great, because it means you likely dont need a collider on the moving object (at least for this to work), and it also means your collision detection is driven using the layerMask at the top there. You just need to make sure you set up your layers and check only the applicable ones in the layerMask.
Hope this helps! = )
This assumes your object is a point, which it almost certainly isn't.
Sure, but if it isn't then you can generally use a spherecast or capsulecast. If not, then no, this wont work, you're right. But if it is a small object, like a bullet, then a point cast is most efficient, clean, and simple. And if it's something like a character then a capsule cast should work fine as well.
Answer by JamesWaterville · Nov 27, 2017 at 09:26 PM
If you have a rigidbody component attached to your high speed game object, you can set the "Collision Detection" to "Continuous". Continuous collision detection is recommended by Unity if you are having issues with high velocity objects. Using the Continuous setting will have a big impact on Unity's physics performance so only apply it when absolutely necessary.
Depending on what you're trying to collide with you may need to experiment with different collision detection settings. Please refer to the following Unity documentation: https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html.
Answer by Hoeloe · Dec 02, 2013 at 12:03 AM
You'd have to do your own collision detection. The problem with this issue is that things don't really "move" in your game - they jump between discrete points when the frame updates. This means that if they're moving too fast, they can be on one side of an object in one frame, and jump to the other side when the frame refreshes. There's nothing you can do about this - it's just a fact of how computer simulation works.
What you can do is approximate where the object went in that last frame. The reason you're not getting collisions here is that you're only testing where the object actually is at any given moment. What you really need to test is all the locations the object was between the current frame and the last one. This is quite a lot more complicated, and you really have to estimate it, since you don't know for certainty exactly how the object travelled between the two points you have - only that it did. What you'll really need to do is create some sort of mathematical description of your collider, and "stretch" it between the points, so they it encompasses everything it would have hit in between. Linear interpolation is usually a reasonable approximation, and pretty easy to do, at least for box colliders.
Here's an example of how it might work, and hopefully it illustrates the problem and the solution reasonably well. For other colliders, you may want to use a more complex method, or just sample the collider with a bunch of linecasts:
Your answer
Follow this Question
Related Questions
How to Set a cut off point where I cant click on my object 1 Answer
Raycast and actions. 1 Answer
Raycast causes all enemies to attack 1 Answer
Create a method that gets called everytime something happens, passing a Transform component to it 1 Answer
I don't know why i can't detect by ray sth. tagged, 1 Answer