Pushing away rigidbody from the direction of raycast hit

So I’m working on a game where the characters are on top of a rigidbody and they push each other away using a fire key. I have a raycast so that I can determine which direction the enemy should be pushed away. So far the raycast detects but when I hit the fire button it returns “MissingMethodException: UnityEngine.Rigidbody.AddForceAtPoint”. Also if it helps the object being hit by the raycast isn’t the rigidbody itself but the game object on top of it.

var ball:Rigidbody;
var bulletForce: int = 100;

function Update () {
var hit:RaycastHit;
  var fwd = transform.forward;
  Debug.DrawRay(transform.position,fwd ,Color.red);
  if (Physics.Raycast (transform.position, fwd, hit)) {
  	print ("There is something in front of the object!");
  	if(hit.collider.tag == "Enemy2"){
   		print(hit.point); //For testing the hit point 
   		if(Input.GetButton("Fire1")){
				print("Shit works!");
				ball.AddForceAtPoint(bulletForce * fwd,hit.point);
   		}
  	}
  }
  else{
  print ("Nothing there!");
  }
 }

You don’t seem to be assigning a rigidbody anywhere in your code. The hit contains a GameObject it hit, so simply use a GetComponent() on that object. Otherwise the code seems alright, but I would recommend first checking for button press and then creating rays and using raycasts, otherwire it is just raycasting pointlesly at every update. Hope this helps.:slight_smile:

EDIT:
I write in C# and if it is not wastly different, the rigidbody component has to be assigned to the variable, not merely declaring the variable as a Rigidbody. Also, there is an addForceAtPosition (not addForceAtPoint) method in the Rigidbody component.