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
1
Question by Coreyf716 · Nov 03, 2012 at 11:26 PM · rotationrigidbodyraycasttransform

Rigidbody.AddForce with Raycast?

 if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, range)) {
     if (hit.collider.attachedRigidbody) {
         hit.rigidbody.AddForce (Vector3.forward * force);
     }
 }

I assumed adding a rigidbody force would take into account the direction the ray was coming from, but I was wrong. I've tried Quaternion.identity and transform.localRotation, but the compiler doesn't understand it. How can I add force to a rigidbody in a forward direction from the point that the rigidbody was hit?

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
1
Best Answer

Answer by FLASHDENMARK · Nov 03, 2012 at 11:38 PM

Are you looking for rigidbody.AddForceAtPosition?

Comment
Add comment · Show 9 · 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 Coreyf716 · Nov 04, 2012 at 10:04 PM 0
Share

Is this right?

hit.rigidbody.AddForceAtPosition (hit.transform.position, transform.position * hitForce);

avatar image FLASHDENMARK · Nov 05, 2012 at 01:38 PM 0
Share

No. You cannot describe a RaycastHit's position by using 'hit.transform.position'. You will have to use 'hit.point' ins$$anonymous$$d.

avatar image Coreyf716 · Nov 05, 2012 at 03:57 PM 0
Share

Would this work?

hit.rigidbody.AddForceAtPosition (transform.TransformDirection (Vector3.forward) hitForce Time.deltaTime, hit.point);

var hit : RaycastHit; var hitForce : float;

avatar image FLASHDENMARK · Nov 05, 2012 at 05:54 PM 0
Share

That is not entirely right either. If you take a look at the documentation you will see that the function takes a 'force' and a 'position' parameter, (but remember you will have to convert the force into a Vector3). The force is of course the amount of force you wan't to apply. And the position is where the force is added.

This is how I would write it:

hit.rigidbody.AddForceAtPosition(transform.forward * force, hit.point);

avatar image Coreyf716 · Nov 05, 2012 at 10:01 PM 0
Share

Aren't you converting the Vector3 into a force by multiplying it by hitForce? I don't see any difference in using 'transform.forward' and 'transform.TransformDirection (Vector3.forward)'.

Show more comments

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

10 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

Related Questions

Object slope rotation issue 0 Answers

Unity Rotate Raycast on Quaternion 1 Answer

Rotating Rigidbody properly using AddRelativeForce 0 Answers

Player character X rotation going haywire on game start 1 Answer

Cant understand why object rotates when setting transform.up to normal 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