Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
avatar image
0
Question by ravelbs · Nov 28, 2018 at 01:58 AM · knockback

Knockback based on enemy position

Hello guys, I was able to code a knockback based on the player movement, but I wanted one based on the enemy position.

Here is my current collision code on the object named "Cactus":

     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.tag == "Player" && theplayer.cantakedamage == true)
         {
             switch(gameObject.tag)
             {
                //if its a trigger 'Dano1', play script hurtplayer(damage1)
                 case "Dano1":
                 FindObjectOfType<HealthController>().HurtPlayer(damage1);
                 break;
             }
         }
     }

This code plays the script to damage calculation and plays the 'knockback' void on the player:

     public void HurtPlayer(int damage1)
         {
         currenthealth -= damage1;
         //plays the script 'knockback' on the player
         thePlayer.Knockback(new Vector3(0,0,0));
         }

This code now applies knockback code on the player based on its actual speed:

    public void Knockback(Vector3 direcaoknock)
    {    
          //direcao is the variable for movement on my character
          direcao += new Vector3(-direcao.x * knockbackforce, direcao.y + (knockbackforce * 10), -direcao.z * knockbackforce);
     }

I want to apply the knockback relative to the object named "Cactus".

Thanks!

Comment
Add comment · Show 1
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 ravelbs · Nov 30, 2018 at 11:35 AM 0
Share

Bump Still no clues about it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by parad0x · Jan 29 at 11:03 PM

You can use the normal of the other you got from OnTriggerStay i think.

 ContactPoint2D contact = other.GetContact(0);
 // Multiplying with -1 coz we need to apply force on the opposite direction of the normal
 Vector2 forceToBeApplied = new Vector2(knockBackXForce * contact.normal.x * -1, knockBackYForce * contact.normal.y * -1);
 targetBody.AddForce(forceToBeApplied, ForceMode2D.Impulse);


Not sure if the API for the GetContact is the same, but you get the idea

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

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

157 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 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

2D Knockback problem 2 Answers

2D Melee with knockback based on the "OverlapCircleAll" function 1 Answer

Help with 2D topdown knockback 0 Answers

Script not working as it should 1 Answer

how to make knockback and slowmotion when hit by enemy 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