Trying to code a knockback but the player is teleporting instead.
I'm trying to code a knockback that pushes the player away from a guided missile when it hits him, but instead of knocking the player away it's teleporting him in the direction and float i made when the missile hits, i tried using Forcemode.Impulse but it didn't work.
the knockback is being coded in the player controller script, thanks for any help in advance.
//Knockback publics
public GameObject RedMissile;
public Vector3 Direction;
public float knockback = 10000;
// Knockback
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "deadlyRed")
{
Direction = RedMissile.transform.position - myRB.transform.position;
myRB.AddForce(Direction.normalized * -knockback);
}
}
A few questions, from common issues with add force:
Does that knockback value (10000) make sense relative to the mass of the rigidbody? If the mass is 1, for example, that seems like a huge value. Should it be smaller?
Are you setting the same rigidbody's velocity elsewhere? Setting the velocity can overwrite forces being applied. It could be that a huge force applies for a split second, then is taken away by setting the velocity, making it look like the player teleports.
Thank you for replying to my question, If i use any value the player teleports a short distance or just about the front of where the collison occured, i used that value to test if that was the case, but it kept doing the same thing. if it helps ill post the more of the below since i can't post the full script because of the character limit:
Rigidbody2D myRB;
SpriteRenderer myRenderer;
bool facingRight = true;
Animator myAnim;
bool can$$anonymous$$ove = true;
//move
public float speed;
//jump
bool grounded = false;
float groundCheckRadius = 0.2f;
public Layer$$anonymous$$ask groundLayer;
public Transform groundCheck;
public float jumpPower;
void Update()
{
if (grounded && Input.GetAxis("Jump") > 0)
{
myAnim.SetBool("isGrounded", false);
myRB.velocity = new Vector2(myRB.velocity.x, 0f);
myRB.AddForce(new Vector2(0, jumpPower), Force$$anonymous$$ode2D.Impulse);
grounded = false;
}
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool("isGrounded", grounded);
float move = Input.GetAxis("Horizontal");
if (can$$anonymous$$ove)
{
if (move > 0 && !facingRight)
Flip();
else if (move < 0 && facingRight)
Flip();
myRB.velocity = new Vector2(move * speed, myRB.velocity.y);
myAnim.SetFloat("$$anonymous$$oveSpeed", $$anonymous$$athf.Abs(move));
}
else
{
myRB.velocity = new Vector2(0, myRB.velocity.y);
myAnim.SetFloat("$$anonymous$$oveSpeed", 0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612153345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collider gets sometimes stuck at wall 0 Answers
Having my GameObject passing trough floor during one frame ( 2D Platformer ) 1 Answer
How to add force based on surrounding colliders in 2D? 0 Answers
How to move rigidbodies with 2D Character controller (No rigidbody) 1 Answer
I want to implement double jump function on my character but everything i try fail 0 Answers