• 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
Question by AsAnAsterisk · Jun 28, 2017 at 12:03 AM · rotationscripting problemmovementrigidbodyai

How to Add Knockback Force Based on What Rotation it Came From

Whenever an enemy does a certain attack and it connects, I want the player to be launched backwards. The current code I have works, but it moves them based on what X Y and Z are put as, whereas I'd like it so that force is applied based on what rotation the enemy was in when they struck the player.

 public class EnemyKnockbackHit : MonoBehaviour {
 
     public Rigidbody playerRigidbody;
     public Slider healthbar;
     public Vector3 moveDirection;
     
     void OnTriggerEnter(Collider attack)
     {
       if (attack.gameObject.tag == "KnockbackHit") {
      
           healthbar.value -= 20;
           playerRigidbody.AddForce( moveDirection * -500f);
          }
          
       
      }
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by aldonaletto · Jun 28, 2017 at 04:23 AM

Do you mean to receive a knockback force from the direction the enemy is relative to the player? If so, just find the vector player position - enemy position and assign it to moveDirection:

 void OnTriggerEnter(Collider attack){
     if (attack.gameObject.tag == "KnockbackHit") {
         healthbar.value -= 20;
         moveDirection = playerRigidbody.transform.position - attack.transform.position;
         playerRigidbody.AddForce( moveDirection.normalized * -500f);
     }
 }

     





Comment
AsAnAsterisk
Neoletum
kakasss

People who like this

3 Show 9 · 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
avatar image Neoletum · Sep 11, 2017 at 02:57 PM 0
Share

Hey, I tried several times to invert this logic to push the enemy away from my player but it won't work - I'm totally confused now, can you help me please?

I just attached this code to my player and assigned the player rigidbody to it but everytime I hit the enemy my player is pushed back and not my enemy O.o

avatar image aldonaletto Neoletum · Sep 12, 2017 at 12:23 PM 0
Share

You must apply force to the enemy's rigidbody and calculate the direction enemy-player. Don't know how you're getting info about your enemy, but assuming that you have some reference to it in variable enemy (transform, collider, rigidbody etc.), the code should be something like this:

  Vecto3 moveDirection = enemy.transform.position - player.transform.position;
  enemy.rigidbody.AddForce( moveDirection.normalized * 500f);

NOTE: I've just noticed that the question and my answer used a force -500f, what would reverse the actual direction. Use a positive force (500f) instead, as shown in the code above.

avatar image Neoletum aldonaletto · Sep 12, 2017 at 02:40 PM 1
Share

Thank you for your attention, I tried it like this:

  public Vector3 moveDirectionPush;
  public Rigidbody2D EnemyRigidbody;
  void OnTriggerEnter2D(Collider2D other)
      {
          if (other.gameObject.tag == "Enemy")
          {
              moveDirectionPush = EnemyRigidbody.transform.position - other.transform.position;
              EnemyRigidbody.AddForce(moveDirectionPush.normalized * 8000f);
          //damage dealing and effects
           }
      }

use AddForce on a GameObject (Enemy) to push it back by hitting it, the script is implemented in a Knockback Class which is attached to the enemy but when I hit it gets teleported back and don't slide slowly back.. I don't have any idea how i could solve this, maybe with Time.deltaTime to move it slowly back? Another strange issue is that my Player only need " 300 " to get pushed back but the enemy needs "moveDirection.normalized 8000f" to get pushed back^^

Show more comments
avatar image

Answer by kritoa · Jun 28, 2017 at 04:52 AM

Not entirely sure what you mean from the wording of your question, but I assume you are trying to get an effect so that if the enemy is moving straight at the character the character will be knocked back, and if the enemy is spinning really fast and moving slowly, then you'd want the character to be knocked sideways, and if both spinning and charging, the character should be knocked back and sideways?

If so, I t$$anonymous$$nk what you are looking for is https://docs.unity3d.com/ScriptReference/Rigidbody.GetPointVelocity.html , w$$anonymous$$ch will return the velocity of a point in world space of a rigidbody, taking into account the angular velocity. So you'd find the impact point of the enemy and your character and ask your enemy's rigidbody what the point velocity is at that point. Then you'd use AddForceAtPosition on your character to knock it in the correct direction (possibly spinning it as well!)

If you don't have a rigidbody for the enemy, respond in the comments and I can write down the math for you (but I assume you have a rigidbody since your enemy is rotating).

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 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

Related Questions

AI walk through solid walls 1 Answer

Help regarding "Collect Cubes" Like Player Movement and Rotation 1 Answer

Rigidbody.MovePosition doesn't move reliably? 1 Answer

Help making orbiting planets,How to rotate around the objects with the largest mass? 0 Answers

Why is my vertical movement faster than my horizontal? 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