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 /
avatar image
0
Question by SolarChaser13 · Jul 23, 2017 at 07:09 PM · collisionrigidbody3daddforce

How can I apply a force to an object in the opposite direction of the object it's colliding with?

I'm trying to code an object in my project to fly back when it collides with a wall. However, when I try to apply this force to my object's rigidbody, I get all sorts of errors. Can someone help me understand how to solve this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjectGravity : MonoBehaviour {
 
     public float roomForce = 50f;
 
     public bool isShot;
 
     public Rigidbody rb;
 
     // Use this for initialization
     void Start () {
 
         rb = GetComponent<Rigidbody> ();
         rb.useGravity = true;
         
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         if (isShot == true) 
         {
             NoGravity ();
         }
 
 
 
     }
 
     public void NoGravity () 
     {
         GetComponent<Rigidbody> ().useGravity = false;
         GetComponent<Rigidbody> ().isKinematic = false;
     }
 
     void OnCollisionEnter (Collision col)
     {
         if (isShot == true) 
         {
             if (col.gameObject.tag == "Room") 
             {
                 //Troublesome code
                 col.rigidbody.AddForce (-col.rigidbody * roomForce);
             }
         }
     }
 }
 
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
2
Best Answer

Answer by abers1997 · Jul 23, 2017 at 10:41 PM

@SolarChaser13 If you want to simply AddForce on the opposite direction of your walls, you'll need to define which wall your're colliding with. Say +z is North; I would tag North, South, West, East walls accordingly.

Like so: alt text

What you'll want to do next is specify "if object collides with N/S/W/E Wall, apply force in the opposite direction". We'll detect collision with OnCollisionEnter and use the NorthWall/SouthWall/WestWall/EastWall tags to define which direction we'll want to inverse on collision.

Like so (C#):

var Rigidbody rb;

var float force;

     void OnCollisionEnter(Collision col)
     {
         //if gameObject collides with "NorthWall", push South (transform.forward * -force)  
         if (col.gameObject.tag == "NorthWall")
         {
             rb.AddForce(transform.forward * -force, ForceMode.Impulse);
         }
         //if gameObject collides with "SouthWall", push North (transform.forward * force)  
         if (col.gameObject.tag == "SouthWall")
         {
             rb.AddForce(transform.forward * force, ForceMode.Impulse);
         }
         //if gameObject collides with "WestWall", push East (transform.right * force)  
         if (col.gameObject.tag == "WestWall")
         {
             rb.AddForce(transform.right * force, ForceMode.Impulse);
         }
         //if gameObject collides with "NorthWall", push south (transform.right * -force)  
         if (col.gameObject.tag == "EastWall")
         {
             rb.AddForce(transform.right * -force, ForceMode.Impulse);
         }
     }
 }

Apply this script to the gameObject that is supposed to be pushed around when collision occurs (make sure it contains a Rigidbody component).

Additionally, you can add an initial directional push by adding this to FixedUpdate() in your script:

var float initialForce;

     void FixedUpdate()
     {
         //push North (z)
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             rb.AddForce(transform.forward * initialForce, ForceMode.Impulse);
         }
         //push South (-z)
         if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             rb.AddForce(transform.forward * -initialForce, ForceMode.Impulse);
         }
         //push West (-x)
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             rb.AddForce(transform.right * -initialForce, ForceMode.Impulse);
         }
         //push East (x)
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             rb.AddForce(transform.right * initialForce, ForceMode.Impulse);
         }
     }
 

1.png (48.8 kB)
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 SolarChaser13 · Jul 23, 2017 at 10:59 PM 0
Share

Thanks! This works great!

avatar image
1

Answer by toddisarockstar · Jul 23, 2017 at 11:04 PM

there is a couple things wrong with your troublesome line. first of all, you have a negative sign in front of the rigidbody. second of all the addforce function needs a vector the value.... not a float. if you want something to bounce backwards replace the line with this:

 rb.AddForce (-transform.forward * roomforce);

this should eliminate your errors. careful though if you are moving your character with velocity, the movent script may override this.

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

126 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

Related Questions

Unity 5 Sphere Rigidbody getting weird behaviour while moving around in a tiled mesh collider scene 1 Answer

Sphere vs Capsule collider collision resolution 0 Answers

Collision with world object and object in Hand 0 Answers

Fermare la Main Camera quando colpisce un muro 3D 0 Answers

jump on collision weird behaviour 2 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