3

When it try to add a Force to my Player it moves and then gets teleported back. Do i need to use a RPC ?

New Code:

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Weapon"))
    {

        rb.AddForce(Vector3.back * knockbackStrength, ForceMode.Impulse);


    }
}
face cem
  • 41
  • 2
  • Who is adding the force to what? You only may move your local player you own (which is then synced to others) .. is it possible that you are trying to move a player that doesn't belong to you and thus it gets overruled by the owner of the object when it is synced again? – derHugo Dec 01 '20 at 21:14
  • A weapon is adding the force to the player and I think I need an RPC but how can I do that? – face cem Dec 02 '20 at 19:20
  • What I mean by my question: Is the physics applied locally on the local player .. or in other words is it happening on the device this player belongs to? Or is this trigger happening on a device this player does not belong to -> has no authority to overwrite the position -> gets synced position from the owner of the player – derHugo Dec 02 '20 at 19:24
  • It's being applied locally. – face cem Dec 02 '20 at 19:25

1 Answers1

-1

I would personally write this as:

public Rigidbody rb;
public float knockbackStrength;

void OnTriggerEnter(Collider collision)
{
    if (collision.collider.tag == "Player")
    {

        rb.AddForce(collision.position, ForceMode.Impulse);


    }
}

As I haven't seen the rest of your code, I've added in the variables, and please could you clarify what the 5 and 0 parameters in the rb.AddForce() function is trying to achieve? I've taken them out because I never have them in. but it all depends on the desired outcome. Try it this way anyway, it might work.

lrainey6-eng
  • 183
  • 1
  • 1
  • 8
  • 1
    So i did some fixing and the player moves now but it's extremely buggy because he gets teleported to his original position after he moves. The new Code is in the question Edit: Forgot to say Thanks – face cem Dec 01 '20 at 15:11
  • no problem - that's weird, what version is your Unity? Edit: I'm not sure why this isn't working, but maybe you could try, after detecting a collision, using a `Debug.Log()` after the `rb.AddForce`. – lrainey6-eng Dec 01 '20 at 19:19
  • I'm using 2019.4.8f1. The collision detection is working fine. I think there is a problem with the synchronisation but i can't find the exact problem. – face cem Dec 01 '20 at 19:58
  • @Irainey6-eng so you personally would introduce worse performance and more error prone `==` check instead of using the better `CompareTag` which OP already had and also introduce compiler errors since [`AddForce`](https://docs.unity3d.com/2019.4/Documentation/ScriptReference/Rigidbody.AddForce.html) doesn't take `float, Vector3, ForceMode` as arguments but only `Vector3, ForceMode` and passing in a position as force makes no sense anyway... Did you mean [`AddForceAtPosition`](https://docs.unity3d.com/ScriptReference/Rigidbody.AddForceAtPosition.html)? – derHugo Dec 01 '20 at 20:49
  • [`AddExplosionForce`](https://docs.unity3d.com/2019.4/Documentation/ScriptReference/Rigidbody.AddExplosionForce.html) as OP had it was also totally fine .. maybe you should check out the API yourself in order to know what which parameter stands for .... the `5` was originally the `explosionRadius` and the `0` the `upwardsModifier` ... – derHugo Dec 01 '20 at 21:11