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 /
avatar image
0
Question by theoyoshimura · Jan 30, 2020 at 05:47 PM · 2d gameunity 2daddforce2d-physicstopdown

How do I add force to a RigidBody2D?

So I have this scene, where I have a player that can shoot bullets at an enemy. I want to AddForce to the RigidBody2D of the enemy when the bullet hits, but until now I have no success. My bullet script:

  void Update()
 {
     theRB.velocity = transform.right * speed;
 }

 private void OnTriggerEnter2D(Collider2D other)
 {
     Instantiate(impactEffect, transform.position, transform.rotation);
     Destroy(gameObject);

     if (other.tag == "Enemy")
     {
        other.GetComponent<EnemyHealthController>().DamageEnemy(damageToGive);
     }

 }

Where do I put the Addforce function? Do I have to get a reference from the enemy RigidBody on my Bullet Script? I'm kinda new to unity so any help is much appreciated! Thanks in advance. PS: I'm trying to make to a topdown shooter game.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by LeFlop2001 · Jan 30, 2020 at 10:00 PM

Adding impactforce on collision is part of the unity engine so you won't have to script anything. But since you're using the OnTrigger function im assuming that either the enemy or the bullet is a trigger in which case there won't be a collision. you can simply use the OnCollision function instead

Comment
Add comment · Show 1 · 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 theoyoshimura · Feb 06, 2020 at 09:38 PM 0
Share

private void OnTriggerEnter2D(Collider2D other) {

     Instantiate(impactEffect, transform.position, transform.rotation);
     Destroy(gameObject);

     if (other.tag == "Enemy")
     {
         Vector3 bulletDir = this.gameObject.transform.right;
         other.GetComponent<EnemyHealthController>().DamageEnemy(damageToGive);
         other.GetComponent<Rigidbody2D>().AddForce(bulletDir.normalized * bullet$$anonymous$$nockback, Force$$anonymous$$ode2D.Impulse);
     }
 }

I got it to work with this bit of code! Thanks!

avatar image
0

Answer by logicandchaos · Jan 31, 2020 at 03:06 AM

other is your enemy reference. So to add force to it it would right after line 13, you would add other.GetComponant().AddForce(theRB.velocity); to add the force of the bullet to the enemy.

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

159 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 avatar image avatar image

Related Questions

OnTriggerEnter2D(Collider2D other) 2 Answers

Way to achieve 3 lane mechanism in a 2D game 0 Answers

Adding force to mouse position adds force relative to screen center 0 Answers

Does anyone know how to fix this unity 2d movement isssue? X axis movement not working 0 Answers

Add Force if GameObject is detected in a Polygon Collider 2D 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