Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by superluigi · Jan 08, 2015 at 01:26 AM · addforceatposition

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*/);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superluigi · Jan 08, 2015 at 05:14 PM 0
Share

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

avatar image AcE_fLoOdEr · Jan 09, 2015 at 01:45 AM 0
Share

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.

avatar image superluigi · Jan 09, 2015 at 07:43 PM 0
Share

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

avatar image
0

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() );
 
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superluigi · Jan 09, 2015 at 01:29 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges