- Home /
Trouble with rigidbody.AddForce
Okay, I am very new to C# and Unity so I appologise in advance for any simple mistakes on my part. I am trying to make a program that will check every object within 10units of the base object and then AddForce in accordence with which side it is on.
My trouble is that I need an object reference for ` RaycastHit.rigidbody.AddForce(Vector3.forward * 5);`
I dont quite know what it means by that other than i might need to bring the hit object's name into another var and put that at the start of RaycastHit.rigid... but when i tried puting the name into a var, it gave me the same error "An Object reference is required for the non-statice feild, method, or property `UnityEnigine.RaycastHit.rigidbody.get`
Fixed in stead of trying to do each rigidbody in the bodies array, i do it for each hit(from the raycasthit)
foreach(RaycastHit hit in bodiesf){
if(hit.distance>1){
float Boxdis = Mathf.Sqrt(hit.distance);
hit.rigidbody.AddForce(-transform.forward * Boxdis * mass);
}
}
using UnityEngine;
using System.Collections;
public class cubebehavior : MonoBehaviour {
public void Update()
{
//Arrays for each direction
RaycastHit[] bodiesF;
RaycastHit[] bodiesB;
RaycastHit[] bodiesR;
RaycastHit[] bodiesL;
bodiesF = Physics.RaycastAll(transform.position, transform.forward, 10);//forward
bodiesB = Physics.RaycastAll(transform.position, -transform.forward, 10);//back
bodiesR = Physics.RaycastAll(transform.position, transform.right, 10);//right
bodiesL = Physics.RaycastAll(transform.position, -transform.right, 10);//left
foreach(RaycastHit rigidbody in bodiesF)
{
RaycastHit.rigidbody.AddForce(Vector3.forward * 5);
}
}
}