- Home /
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);
}
}
}
}
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:
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);
}
}
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.
Your answer
Follow this Question
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