- Home /
Unrealistic Fps Shooting?
Right now, I added this code to my camera
function Update() {
if(Input.GetMouseButton(0))
{ var forward = transform.TransformDirection(Vector3.forward); var hitInfo : RaycastHit; if (Physics.Raycast (transform.position, forward, hitInfo, 1000)) {hitInfo.rigidbody.AddForce (forward * 50); } }
This isn't good because it just tells the rigidbody to go the opposite direction where ever the bullet hit it. I want the rigidbody to react as if a bullet rigidbody hit it. How can i do this? Thanks!
Answer by Kourosh · Apr 16, 2011 at 05:02 PM
Try AddForceAtPosition instead of AddForce and place the hit position in its argument. Now you are adding force at the pivot, no matter where the bullet hits.
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForceAtPosition.html
Edit:
if(Input.GetMouseButton(0))
{ var forward = transform.TransformDirection(Vector3.forward);
var hitInfo : RaycastHit;
if (Physics.Raycast (transform.position, forward, hitInfo, 1000)) {
var hitDirection:Vector3 = (hitInfo.point - transform.position).normalized; // the bullet direction.
hitInfo.rigidbody.AddForceAtPosition (hitInfo.point,hitDirection * 50);
} }
the hit direction is multiplied by 50. I'm not sure but that could be a great force. depends on your object as well.
because you need to pass the hitInfo.transform.position to the AddForceAtPosition. Is this what you are doing?
no what do you mean by passing the hitinfo.transform.position? How i do this?
ok thank you it says that point is point is not a member of unity.transform?
Your answer
Follow this Question
Related Questions
raycast not doing good 1 Answer
How to Implement First Shot Accuracy? 2 Answers
Raycasr in my fps? 1 Answer
How can i make a raycast in my fps game? 1 Answer
FPS: Make animated character weapon follow camera movement 1 Answer