- Home /
addForceAtPosition help
I'm having trouble getting the results I want with addForceAtPosition. The following script is attached to my characters fist and it's used to punch objects. However, the physics are all wrong. Currently I have a really tall pillar that I am punching. Obviously because it's really tall I am punching it at the bottom, but when I punch it the pillar flies away straight. I was hoping it would rotate correctly and start rotating in a motion similar to a backflip, but there is no torque being applied. I guess I am confused as to how exactly addForceAtPosition works. Here's my script
var player : GameObject;// used for the direction of flight
function OnCollisionEnter(other : Collision)
{
if(other.gameObject.name == "Pillar")
{
other.rigidbody.AddForceAtPosition(player.transform.forward * 100, transform.position /*this code is attached to my characters hand, so the transform.position should be my hands position or, from my understanding, my hands location at the time of impact*/);
}
}
Answer by AcE_fLoOdEr · Jan 08, 2015 at 04:01 AM
Try using
other.gameObject.transform.localEulerAngles = new Vector3(x: 0, y: 0, z: 0);
obviously find the appropriate angle that you want and change its value. That's how I would do it for a back flip. And if you really want random speeds to give it a more realistic effect, consider using
float xRotation = Random.range(0, 10);
something like that. Hope this helps or at least bring you closer to your answer.
Yea I get what you mean, but what I'm looking for is realistic physics. You know like if you kick someones legs they'll fall backwards but if you kick someones head they'll fall forward. The problem with setting the rotation myself is that I don't always know where the object will be hit, so the rotation could end up being all wrong
Okay there is something else you could do. It's sloppy, but take it as a temporary solution.
Add colliders to each part of the body you want to hit, you know, like hit boxes. Tag the one near the top "top collider", "second top collider" and what not. And OnCollisionEnter, check for which collider it hit and rotate/transform the pillar accordingly.
That's a good solution actually. I'm not gonna use it for humanoid players, I'm gonna use it for objects (boxes, pillars, etc). It'll still work though. Like you said it's kinda sloppy but it should look somewhat realistic
Answer by RetepTrun · Jan 08, 2015 at 09:59 PM
I believe you should change player.trans... to just transform.forward. Unless the hand transform is being applied to the player variable. But even then the blue forward arrow of the hand transform may not be in the correct direction
See this piece of my gun script. Blue Z arrow is the direction of force.
if(canFire && loaded && !betweenShots){//actual firing
//print ("fire");
Vector3 dir = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
if (Physics.Raycast( transform.position, dir*range, out hit)){
//print ("hit");
if(hit.rigidbody){
//print ("rigid");
hit.transform.rigidbody.AddForceAtPosition( dir*forcePerShot , hit.point , ForceMode.Impulse);
}
else if(hit.transform.root.rigidbody){
//print ("rigid");
hit.transform.root.rigidbody.AddForceAtPosition( dir*forcePerShot , hit.point);
}
hit.transform.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
ammo--;
shotCount++;
StartCoroutine( WaitTillNextShot() );
}
Answer by DanSuperGP · Jan 08, 2015 at 10:18 PM
Rather than applying the force from the hand's location, use the contact point of the collision.
http://docs.unity3d.com/ScriptReference/Collision-contacts.html
This will ensure that the force is being applied from a point on the surface of the pillar.
Gotta admit I tried this but could never get past the damn console. The examples in the docs confuse me. I don't understand how the whole contacts thing works.
Your answer
Follow this Question
Related Questions
Add force at position Question 1 Answer
My game lags on iOS devices and not android 0 Answers
AddForceAtPosition moment of force not correct work 0 Answers
ApplyForceAtPosition-Problem 1 Answer
Add force with direction 2 Answers