Home > front end >  Add Force teleporting the player
Add Force teleporting the player

Time:08-03

I want to push back my player when it collides with an object but when it collide with the object, it just teleporting back rather then pushing smoothly. I tweaked with values like Mass, Drag on player's rigidbody or knockbackStrenght value from script. It just teleporting further positions with higher values and teleporting to closer positions with lower values but it always teleports not pushback.

My code on the object that will push back player looks like:

public class StickRotator : MonoBehaviour
{
    [SerializeField] float rotateSpeed;
    [SerializeField] float knockbackStrenght;
    [SerializeField] Vector3 rotateDir;

    Vector3 _parentPos;

    void Start()
    {
        _parentPos = GetComponentInParent<Transform>().position;
    }

    void Update()
    {
        transform.RotateAround(_parentPos, rotateDir, rotateSpeed * Time.deltaTime);
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            other.gameObject.GetComponent<Rigidbody>().AddForce
                (Vector3.back * knockbackStrenght, ForceMode.Impulse);
        }
    }
}

Player's rigidbody settings

When player collide with stick, it just teleporting back rather then pushing back

CodePudding user response:

Along with @absinthe's comment, you could try using rigidbody.velocity instead of AddForce.

CodePudding user response:

I find out that Animator component on the player causing the issue. Unticking 'Apply root motion' seems solved my problem but now when player pushing back smoothly, its jittering.

  • Related