Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by LeGoMe · Feb 24, 2018 at 09:11 PM · 2d-platformer2d-physicsknockback

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);
         }
     }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alanisaac · Feb 24, 2018 at 09:22 PM 0
Share

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.

avatar image LeGoMe Alanisaac · Feb 24, 2018 at 09:53 PM 0
Share

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);
         }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges