Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

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

$
0
0
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 (); rb.useGravity = true; } // Update is called once per frame void Update () { if (isShot == true) { NoGravity (); } } public void NoGravity () { GetComponent ().useGravity = false; GetComponent ().isKinematic = false; } void OnCollisionEnter (Collision col) { if (isShot == true) { if (col.gameObject.tag == "Room") { //Troublesome code col.rigidbody.AddForce (-col.rigidbody * roomForce); } } } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>