- Home /
Instant transform.position and collision
So here is the context: I'm making a little Airhockey game, where the player controls the pusher by moving the mouse. I got it working by the following "pseudo-code":
transform.position = mousePosition;// i.e. pusher's position = mouse position, where I mouse position is gotten with Camera.main.ScreenToWorldPoint , no problem on that
Now comes my problem: If the mouse is moved too quickly, it happens that the pusher can find itself inside the puck, and for some reason, not colliding with it.
Here is my collision script:
private void OnCollisionStay(UnityEngine.Collision collision)
{
if (collision.gameObject.GetComponent<MouseMove>() != null) {
direction = transform.position - collision.contacts[0].point;
GetComponent<Rigidbody>().AddForceAtPosition(direction*2, collision.contacts[0].point, ForceMode.Impulse);
}
}
(By the way I just made a custom collision so I can adjust the bouncing power freely)
So that's quite it, I would like to know if there is a way to make the pusher move instantly to the wanted position, but by actually moving, not teleporting (because I believe this is the main reason to my problem) Or if my custom collision function is somehow incomplete.
Here is a screenshot of the "bug":
Answer by hawksandwichgames · Jun 29, 2018 at 01:12 PM
I think what you're looking for is Rigidbody.MovePosition. It "teleports" to the given position, but checks collisions on its way.
Works well thank you :D Now I gotta slow down the puck because he is going crazy x)
Your answer

Follow this Question
Related Questions
Transforming position for different colliders for repeating background? 0 Answers
Object collider not operating properly 2 Answers
Move object after collision 1 Answer
How to stop a character from moving out of player's control? 0 Answers
How to move a Game Object from a script not attached to it. 1 Answer