Stop a rigidbody on collide when dragging it with a mouse / with DontGoThroughThings
Hi,
I'm making a game where you drag sphere game objects across 3 dimensions by snapping to fixed y or z screen values when pressing the "H" key (fixed z - moving the object up when moving the mouse forward/ fixed y - object going further from the camera by moving the mouse forward). The objects x axis on drag is always represented with a left-right mouse movement and it's original position.
I'm setting the object position with transform.position but i can change it to rigidbody.moveposition if it would help me resolve this in a good way, but i would rather not as it screws up other things in the game.
Since I'm moving my object with a mouse and those mouse movements can be fast, I'm sometimes skipping the collision. I've searched the web and most of the successfully solved topics regarding this issue are answered with the DontGoThroughThings Script. After adding the script to my objects they would follow your mouse and in the same time trying to keep the position which the Don'tGoThroughThings is giving it, creating a blinking/glitch effect. This is on high speed collision detected by the script.
I wanted to make this object wait for me to come back for it with the mouse on the collision point that the DGTT script passes him, so I've added a public bool to the DGTT script and set it to false when there's a collision in the DGTT and then not do the transform.position from the drag...And set it to true when mouse leaves object, then doing the drag transform.pos... This works pretty well, besides a few occasional glitches on high speed collisions (Who's cause I can't track down) AND the low speed collisions causing the object to bounce right off (I can target the ball after the bounce while still holding mouse button and gain control of the object again, all in the same click, similar like I do when the object is waiting on the collision spot for the mouse on high speed collisions). The bounce is happening due to the normal collision being detected...
So my bottom line question is: How do I get to control the behavior of the drag collision so that I have balls ALWAYS bounce off a wall type 1 (No matter if the collision is high or low speed, in other words DGTT collision or Standard physics that take place), also to have the balls always wait for the mouse on the collision point with walls type 2, and finally to have the balls stop and drop on collision with walls type 3(I think i got these last two covered but I would like to hear your opinion).
How do I get to control the behavior of the drag collision so that I have balls ALWAYS bounce off a wall type 1. This leaves me without an idea and I'm quite stuck, I can't think of a way how to keep those two different bounces the same in the borderline cases (First High Speed and Last Low Speed bounce)... As i see it i would keep the normal physics bounce and do a addforce for the high speed one after I position it in the collision spot, right? But how......Wait a second...I got something with that... It's really great to put your issue "down on paper", as I discovered many things while trying to formulate the question... I got this Always Bounce behavior to work with this two extra ifs in the top of the DGTT if raycast (executing before standard three cases in the DGTT script):
if (doThisShit2) //execute bounce
{
myRigidbody.useGravity = true;
myRigidbody.position = hitInfo.point - (movementThisStep / movementMagnitude) * partialExtent;
myRigidbody.AddForce(- 100 * (hitInfo.point - (movementThisStep / movementMagnitude) * partialExtent));
doThisShit2 = false; // We've bounced
return;
}
if (hitInfo.collider.name == "Cube")//is collider the collider we want to set the high speed bounce behavior to
{
doThisShit2 = true; // This is the bool that's here to say we need to do a bounce on a high speed collision
doThisShit = false; //This is the first bool I've added, It prevents the drag transform.pos
return;
}
I now have one more issue, now the waiting behavior on high speed collisions for other type of walls stopped working quite so responsively, a few more glitches/blinks than before. I think I need to tweak this a bit, but it seems I'm on the right track. EDIT: and yeah, the angle of the newly added bounce off on high speed collisions is sometimes the one he came from but it's not a 90 degree angle with the wall, it must be something with the vector3 given to addforce call in the code above...
Sorry to waste this much space to ask this thing in the end, but i thought it was important to get the full picture :)
I'll be happy to post additional codes or whatever if anyone finds this useful.
If you have any improvement suggestions, fell free to add a comment or a reply...
Regards,
Lovre
Your answer
Follow this Question
Related Questions
Sphere bouncing back on edges of aligned objects 0 Answers
Restrict held object movement 0 Answers
Position and collider seem to incorrectly shift when moving object 0 Answers
how to prevent a kinematic rigidbody from going through static colliders 0 Answers
Flying character rigidbody - can't see any static collider 1 Answer