- Home /
What is the cheapest way to prevent object A from going through object B
Both object A and object B have a kinematic rigid body attached; Interpolate: Interpolate, Collision Detection: Continous Speculate.
I'm using onTriggerEnter to detect the collision.
The problem is both object A and object B is moving very fast. And as you probably already know the faster an object moves the greater the travel distance between frames. So in one frame, there is a good amount of distance between object A and objectB and in the next frame object A pass-through object B.
My question is, is there a way to detect collision immediately on the impact between object A and object B? I know about raycasting and based on what I learn it's expensive and it's mostly used for extremely fast moving objects like a bullet.
Object A is being moved by the animation on the character's hand and object B is being moved by script. If I was to use OverlapSphere on object A, how would I use the information to stop both objects immediately when they're next to each other.
You can try using "Vector3.distance", keep checking the distance between A & B, if its less than the threshold you move them back to the closet they can get to each other i.e not overlapping each other.
thank you for responding. Okay, say frame 10 the two objects are overlapping and frame 9 there is a moderate amount of distance between them. How can use their distance at frame 10 and frame 9 to calculate the position where they're in contact but not overlapping?
Yes pls share the code..It will make easy for us to analyse issue and solve it
This is how object B is move or rather rotated
Object A is move by the character's animation. It parented to the character's right hand. The animation exits once Object A collide with object B. Which is done in a similar manner with on trigger but ins$$anonymous$$d of changing the rotational direction upon impact I ins$$anonymous$$d change an animation parameter from false to true
Answer by black_sheep · May 30, 2019 at 11:46 PM
Raycasts are only expensive when you use hundreds of them per frame. I'm using =~20 per frame on my project and it does not hurt performance heavily. But what you should use in this case if the collider's bounds. If the closest distance from A to the bounds of B is smaller than the radius of A, than the ball is getting through B. Luckily, unity already has the functions for us. Code would look like:
Collider ColliderB = B.GetComponent<ColliderB>();
float distance = colliderB.bounds.sqrDistance;
if(distance <= A.radius){
//the direction for going away
Vector3 vector = A.transform.position - B.transform.position;
//goes away by the distance to the collider + the radius of the ball
A.transform.position += vector.normalized * (distance + A.radius)
}
Answer by wyatts · May 30, 2019 at 04:17 AM
In 2018.3 Unity introduced Speculative continuous collision detection. That should help with this if you're not already using it.
I may have miss spelled it but I did include "Collision Detection: Continous Speculate"
can you share the code of how you move it? probably that way you can fix it
Answer by gaggedegg · May 30, 2019 at 10:09 AM
@xxTheo
In OnTriggerEnter()
there is a way to detect which object is colliding with which object by checking the tag of colliding object.
Suppose your ObjectA has tag "ObjA", you can check collision by:
private void OnTriggerEnter(Collider coll) { if(coll.CompareTag("ObjA")) { if(gameObject.name == "ObjectB") { Debug.Log("Collision happned with ObjectB "); } }
gameObject.name
will give you the name of the object with which ObjectA is collided.
Hope this help :)
his issue is objects not colliding because they are really fast
Thank you xxmariofer, I'm very grateful for every response I get but do these people actually read what I wrote or did I do a terrible job at explaining the problem.
This is how I'm moving object B
Object A is move by the character's animation. It parented to the character's right hand. The animation exits once Object A collide with object B. Which is done in a similar manner with on trigger but ins$$anonymous$$d of changing the rotational direction upon impact I ins$$anonymous$$d change an animation parameter from false to true
you should get better results using forces, try changing the fixedupdate code to this
rb.angularVelocity = speed * EulerAngleVelocity * Time.deltaTime;
rb.maxAngularVelocity = 99999999;
this will give you more precise collisions, BUT you need to make the rigidbody not kinematic to be affected by forces