Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by sselke42 · Aug 22, 2019 at 01:51 AM · physics2dcollision2dknockback

How do I effectively perform KnockBack whenever my character gets hit by an enemy/ harmful object?

Howdy! I've been working on the knockback function/implementation for my 2D sidescrolling shoot em' up game for about 2 weeks now and I was hoping anyone could help me figure it out? I've been doing nonstop research online, but no matter what I find, my character still seems to just be bouncing up a tiny bit, and not up and to the left (or right, depending on which side the enemy is on) whenever he gets hit by the enemy, I would like my character's knockback to be similar to that of the original Sonic Sega Genesis trilogy , it just feels so smooth, even though you had just been hit by an enemy. The way I have my code set up now is: My Health script has a Hurt function and this hurt function communicates with the PlayerControls2 script to execute the knockback function, but, again, its just bouncing my character in the air whenever he hits an enemy, almost like he's jumping whenever he hits the enemy. Below I have my Health script, and my PlayerControls2 script, thank you for your time and for a potential answer :) P.S: If you need any additional code/ anything you can think of, or need a video of the bug, I will post it here ASAP

 ***//Health*************************************************************************
 
   public int health = 5;
 public int numOfCrystals;
 private PlayerControls2 player;

 public Image[] crystals;
 public Sprite fullCrystal;
 public Sprite emptyCrystal;

 void Start()
 {
     player = FindObjectOfType<PlayerControls2> ();
 }

 void Update()
 {
     if(health > numOfCrystals)
     {
         health = numOfCrystals;
     }

     for (int i = 0; i < crystals.Length; i++) {

         if (i < health) {
             crystals [i].sprite = fullCrystal;
         } else {
             crystals [i].sprite = emptyCrystal;
         }
             
         if (i < numOfCrystals) {
             crystals [i].enabled = true;
         } else {
             crystals [i].enabled = false;
         }
     }
 }

 public void Hurt()
 {
     health--;
     player.knockBack ();
     if (health <= 0) {
         SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
     }
 }

 void OnCollisionEnter2D (Collision2D collision)
 {
     Enemy enemy = collision.collider.GetComponent<Enemy> ();
     if (enemy != null) {
         Hurt ();
     }
 }
   }

  **//PlayerControls2****************************************************

      public float speed;
 public float jumpForce;
 public Transform groundCheck;
 public float checkRadius;
 public LayerMask whatIsGround;
 public int extraJumpValue = 0;
 public float fallMultiplier = 2.5f;
 public float lowJumpMultiplier = 2f;
 public float knockBackForce = 10;
 public bool knockBackNow;
 /*
 public float knockBackLength;
 public float knockBackCount;
 public bool knockFromRight;
 */
 //public Transform currentCheckpoint;
 private float moveInput;
 private Rigidbody2D rb;
 private bool facingRight = true;
 private bool isGrounded;
 private int extraJumps;

 void Start()
 {
     extraJumps = extraJumpValue;
     rb = GetComponent<Rigidbody2D> ();
 }

 void FixedUpdate() //used to manage all the physics in the game
 {
     isGrounded = Physics2D.OverlapCircle (groundCheck.position, checkRadius, whatIsGround);

     if (knockBackNow == false) {
         moveInput = CrossPlatformInputManager.GetAxis ("Horizontal");
         rb.velocity = new Vector2 (moveInput * speed, rb.velocity.y);
     } /*else {
         if (knockFromRight) {
             rb.velocity = new Vector2 (-knockBack, knockBack);
         } 
         if (!knockFromRight) {
             rb.velocity = new Vector2 (knockBack, knockBack);
         }
         knockBackCount -= Time.deltaTime;
     }
     */

     if (facingRight == false && moveInput > 0) {
         Flip ();
     } else if (facingRight == true && moveInput < 0) {
         Flip ();
     }
 }

 void Update()
 {
     if (isGrounded == true) {
         extraJumps = extraJumpValue;
     }
     if (CrossPlatformInputManager.GetButtonDown ("Jump") && extraJumps > 0) {
         rb.velocity = Vector2.up * jumpForce;
         extraJumps--;
     } else if (CrossPlatformInputManager.GetButtonDown ("Jump") && extraJumps == 0 && isGrounded == true) {
         rb.velocity = Vector2.up * jumpForce;
     }
 }


 void Flip()
 {
     facingRight = !facingRight;
     transform.Rotate (0f, 180f, 0f);
 }

 public void knockBack()
 {
     //put knockback code here, since its called in the Hurt function in Health
     rb.velocity = Quaternion.AngleAxis(-70, transform.right) * transform.forward * knockBackForce;
     knockBackNow = true;
     if (isGrounded == true) {
         knockBackNow = false;
     }
 }
 }



Comment
Add comment
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

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

111 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

Related Questions

Calling a Collision from code OR not simulating drag/gravity 0 Answers

Physics2D.CircleCast result doesn't match with actual circle object's collision detection 0 Answers

Why do my Physics2D Settings Reset on Test 1 Answer

How to disable collisions without disabling rigidbody? 1 Answer

Is it possible to find the contact point of a circle collider 2D with another collider inside of OnTriggerEnter2D? 1 Answer


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