- Home /
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!");
}
}
Answer by MolochSim · Dec 04, 2014 at 02:21 PM
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.:)
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.
The first line assigns a rigidbody to the ball, I also need a constant check because the raycast is also going to check distance between the player and the character so having it on a button click wouldn't work.
The method you are trying to use does not exist. I updated my answer.
Your answer
Follow this Question
Related Questions
Raycast not detecting a hit from far away, it only detects when close. 0 Answers
How can I get the tag of a child object? 1 Answer
Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer
Help with Raycast C# 0 Answers
Raycast exit point of collider 0 Answers