Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 easilyBaffled · Apr 17, 2016 at 01:07 AM · physicsrigidbodyaddforcedirectionforce

Why is force only being added in the same direction?

I am trying to build a boxer who is always looking at the player, and punches at the player when in range. The punching is being done with AddForce, with the glove's forward as the direction. No matter what direction the boxer and glove face the glove moves in the same direction (0, 0, -1); This is the code I am using, it's on the boxer which is a parent of the glove.

 gloveRB = glove.GetComponent.<Rigidbody>();
 function Update() {
     transform.LookAt(target.transform);
     var HitInfo : RaycastHit;
     var gloveDirection = glove.transform.InverseTransformDirection(gloveRB.velocity);
     if (gloveDirection.magnitude < 0.1) {
         if (Physics.Raycast(glove.transform.position, glove.transform.forward, HitInfo, 2)) {
             if(HitInfo.transform.gameObject.tag == 'boxer') {                
                 Debug.DrawRay(glove.transform.position, glove.transform.forward * 2, Color.blue, 20);
                 gloveRB.AddForce(glove.transform.forward * 10 * Time.deltaTime, ForceMode.Impulse);
             }
         }
     }


It maybe worth noting that I have a spring joint connecting the glove and boxer so that the glove is drawn back after the punch. I am using ForceMode.Impulse because I only want this force added all at once. The debug ray is drawn in the correct direction, and the glove should follow it but it just moves in (0, 0, -1)

Why isn't the force being added in the right direction? alt text

screen-shot-2016-04-16-at-60407-pm.png (85.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Apr 17, 2016 at 02:16 AM

It appears you simply need to replace

 gloveRB.AddRelativeForce(...);

with

 gloveRB.AddForce(...);


You're already accommodating the relative direction using glove.transform.forward, so there's no need to get a relative direction.

Off the top of my head, I can't think of why the force would be added in exactly the direction it is, let alone exclusively, since I think the rotation should logically be doubling itself, but regardless, there should be no need for a relative force when you're already factoring in the relative angle.

That said, it you wanted to use AddRelativeForce anyway, that could be done by changing it to:

 gloveRB.AddRelativeForce(Vector3.forward * 10, ForceMode.Impulse);
Comment
Add comment · Show 2 · 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 easilyBaffled · Apr 17, 2016 at 02:21 AM 0
Share

Sorry that was my typo. I had been using AddForce, but before posting the question I tried AddRelativeForce and forgot to change it back. Though I did try your suggestion for AddRelativeForce(Vector3.forward which resulted in the glove moving towards the global forward.

avatar image Eno-Khaon easilyBaffled · Apr 17, 2016 at 03:23 AM 0
Share

Hmm... well, there are a few other things you can look at (including an oversight on my part).

First, you mention wanting the glove to launch forward with a full initial force all at once. That means you don't need to multiply by Time.deltaTime in an AddForce call. That said, you really shouldn't apply deltaTime regardless, since it's automatically applied by the two non-immediate Force$$anonymous$$ode choices.

That said, based on these two lines of yours, especially:

 // Removed deltaTime
 Debug.DrawRay(glove.transform.position, glove.transform.forward * 2, Color.blue, 20);
 gloveRB.AddForce(glove.transform.forward * 10, Force$$anonymous$$ode.Impulse);

There is no good reason why the ray and the force application would go in different directions unless there are some additional factors preventing mobility which aren't readily apparent (such as any unexpected colliders blocking movement, rigidbody settings preventing motion, or spring joint adding unexpected restrictions).

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

63 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get result (force & torque) of AddForceAtPosition? 2 Answers

Add Force without Rotation 2 Answers

How can I convert velocity/direction to Force? 3 Answers

add force to object that has 2 different rigid bodies 0 Answers

Returning a rigidbody back to its original x and z rotations through physics forces. 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