P
P
Pickto2017-02-28 13:13:56
C++ / C#
Pickto, 2017-02-28 13:13:56

2D character shake when colliding with colliders, how to fix?

When in contact with objects where there is a collider and moving towards them, the character is thrown back, how to avoid this?

if (Input.GetKey("w")){
      transV = playerSpeed * Time.deltaTime;
    }else if (Input.GetKey("s")){
      transV = -playerSpeed * Time.deltaTime;
    }
    //Horizontal
    if (Input.GetKey("d")){
      transH = playerSpeed * Time.deltaTime;
      if (playerLeft){
        gameObject.transform.localScale =new Vector2(-1f, 1f);
        playerLeft = !playerLeft;
      }
    }else if (Input.GetKey("a")){
      transH = -playerSpeed * Time.deltaTime;
      if (!playerLeft){
        gameObject.transform.localScale =new Vector2(1f, 1f);
        playerLeft = !playerLeft;
      }
    }
    transform.Translate(new Vector2(transH, transV));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Espleth, 2017-02-28
@Pickto

What specifically don't suit you? The character should be pushed away from the colliders.
But try a couple of options:
1) Move the character not through transform.Translate, but through rigidbody.AddForce. I suspect that this is the problem: your character passes into the collider and is pushed back from the collider.
2) Create a physical material for the colliders (including the player) and set the bounciness to 0, but apparently the problem will be solved by the 1st option.

T
TheTalion, 2017-02-28
@TheTalion

You need to enable the isKinematic field on the Rigidbody of the pushed object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question