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);
}
}
}
}
↧