- Home /
this question resolved itself
Player is moving through walls when force is added from a knockback script? *UNANSWERED*
I have a level where I knockback the player when they enter a box collider, using force to push them, and the player can sometimes get shot through the level walls and floor if the knockback object it hit at some angles angle. I do not want this to happen. How can I keep the player from getting thrown through the walls?
Here is the knockback script
public float force = 3;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
Vector3 dir = (col.contacts[0].point - transform.position).normalized;
col.rigidbody.AddForce(dir * force);
}
}
try to lower your FixedTimeStep from Edit>ProjectSettings>Time.
that did not work. but that was a good idea
ins$$anonymous$$d of using dir use:
col.rigidbody.AddForce(-col.rigidbody.velocity * force);
collision detection continuous and unchecked trigger worked well with me. did you put any colliders on wall? if you're using grid tilemap & composite collider will help.
I hope it helps
Answer by myzzie · Sep 01, 2018 at 04:01 PM
On the rigidbody, set the collision detection to continuous
Have you tried different forcemodes? a force with 3 without a forcemode defaults as force, meaning it uses it's mass. You will never a ridiculous low amount of mass to move something with a force of 3 only once. Forcemode.Impulse is used for this sort of thing.
I have tried a few different things like that. And actually im using 3000 force because 3 is doing nothing. 3000 is only kicking the player back by 1 unit on the x axis. I added mass to the player and that didn't solve the issue either. I am wondering if there is a different script that would work better.
Answer by unity_oJKU_foMOUhGRw · Sep 02, 2018 at 10:09 PM
You can add the additionals box colliders of the same size and shape as the walls. That should do it. Aswell check if the "Is trigger" box is unchekd, otherwise it might be a reason behind it.
The is tigger box is not checked. The player is only forced through the walls when a force is added by the knockback
I just added 70 box colliders to all the walls in the area...the player still was forced through it with the knock back. Any other Ideas?
Answer by TabuuForteAkugun · Sep 03, 2018 at 01:24 AM
I'm curious about this physics-related question too, as sometimes I get the same kinds of issue.
I really don't know what is happening. I have looked everywhere for this and cannot find an answer.
Follow this Question
Related Questions
Tricky issue involving knockback and the player. 1 Answer
Click-to-move questions 2 Answers
Issue with GameObject has been destroyed but it is not destroyed 1 Answer
When player hit wall continue moving 0 Answers
Distribute terrain in zones 3 Answers