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
2
Question by Daumas18 · Oct 26, 2014 at 01:50 PM · contact.point

Applying force at contact.point

Hello everyone. I am trying to apply relative force to a rigidbody (cylinder) at its contact point with the ground. The direction of the force applied has to change according to the surface which the cylinder is touching. The pictures show how the direction of the force, applied from rigidbody's contact point(s), should change. Orange sphere is a contact point, yellow arrow - force that should by applied to the cylinder by the script, blue triangle on a cylinder just shows that it is not rotating when clearing the obstacle.

alt text alt text

Current working code:

 function OnCollisionStay (collision : Collision) {
     
     for (var contact : ContactPoint in collision.contacts) {
             
         contacts = collision.contacts.Length;
 
         if(Input.GetKey(KeyCode.W)){
             
         var dir : Vector3;
         dir = (transform.position - contact.point);
         rigidbody.rigidbody.AddForceAtPosition(Vector3.Cross(transform.right, dir).normalized * (Power / collision.contacts.Length) * Time.deltaTime, contact.point);
             
         }
         
     }
 
 }

In this code, made using the suggestion of Habitablaba, I have created a vector (dir) from the center of the cylinder towards the contact point, crossed it with transform.right to create a direction offset and normalized it. Variable "Power" is a float, it is divided by the number of contacts so that the total force does not increase when the rigidbody has more than one contact with the surface.

3dpvz4.jpg (55.9 kB)
3dpvz2.jpg (54.5 kB)
Comment
Add comment · Show 4
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 Scribe · Nov 03, 2014 at 06:49 PM 3
Share

What have you tried?

avatar image Habitablaba · Nov 04, 2014 at 06:19 PM 0
Share

The comments below are getting out of hand, so here is what I suggest.

Update your question with the following:
- Better explanation of what is going on in that picture. (are you just trying to provide 'friction' or 'drag'?) - Code which shows where the contact point is being deter$$anonymous$$ed. - The code from the comments below showing what it is you've tried and a description of how the object behaves when you do that.

I think there is a lot of misunderstanding going on here. Both from you in relation to vectors and from the rest of us in relation to what it is your asking.
Once we get a better understanding of the question, we may be able to provide a better answer.

avatar image Habitablaba · Nov 07, 2014 at 05:49 PM 1
Share

This question is so much improved from the last time I looked at it, so good job there :)

I think the vector you are looking for is:
Take the vector that is from the collider's center to the contact point.
Cross that vector with the object's transform.right.
Normalize this vector, then multiply by your desired power.

avatar image Daumas18 · Nov 08, 2014 at 03:51 PM 0
Share

That worked! Code:

 var dir : Vector3;
 dir = (transform.position - contact.point);
 rigidbody.AddForce(Vector3.Cross(transform.right, dir).normalized * 100 * (Power / collision.contacts.Length) * spindr * Time.deltaTime);

The behavior is definitely correct, but just in case, is there a way to check? Something like Debug.DrawForce... Also you should make this comment in to an answer.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by YoungDeveloper · Oct 26, 2014 at 01:52 PM

Take the collision point and add force forward regarding from center in 90 degree angle.

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 Daumas18 · Oct 26, 2014 at 03:42 PM 0
Share

How do I write this in java code?

avatar image YoungDeveloper · Oct 27, 2014 at 03:52 PM 0
Share

Unity doesn't support Java.

avatar image Daumas18 · Oct 27, 2014 at 06:55 PM 0
Share

All of my codes are .js (java script) files, its not really java, but people call it that way for short.

avatar image Kiwasi · Oct 27, 2014 at 07:02 PM 2
Share

No they don't, they call it JavaScript or UnityScript. Java is a completely separate language. Confusing the two terms pretty much shows everybody you have no idea what you are talking about.

The method you are after is AddForceAtPostion. Friction will also do a similar thing, you can set this on the physics material.

avatar image Habitablaba · Nov 03, 2014 at 07:34 PM 1
Share

I think that you do not understand vectors, or vector math.
What do you expect forceDir to be, when you get the direction from the position of the object to the contact point?
Do you understand what a 'contact normal' is?
Do you understand the concept of 'tangent'?
What do you expect adding 90 to the z will do?

Show more comments
avatar image
0

Answer by Kiwasi · Nov 05, 2014 at 06:55 PM

Still figuring out what you even want.

If you are after the direction of the vector shown in the pic then you need to take the cross product of the contact point minus the centre point and the vector going into the page. I'd suggest normalising the result. This assumes you are working in 2D

Comment
Add comment · Show 5 · 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 fafase · Nov 05, 2014 at 07:08 PM 1
Share

or you have the normal of the hit (x,y) and you get the new vector ( -hit.normal.y, hit.normal.x). :)

avatar image Kiwasi · Nov 05, 2014 at 07:30 PM 0
Share

That's what I just said :)

So assu$$anonymous$$g you are working in normal unity 2D environment the cross product does simplify as described by @fafase.

avatar image Daumas18 · Nov 05, 2014 at 08:25 PM 0
Share

I am working in 3D, I have mentioned that this picture is a side view. I will put this in a different way - this code should apply force to the rigidbody in a similar way as force is applied to a tire, except this rigidbody does not rotate, it just slides along the surface. Hope everyone gets it, I do not have any more ideas how to explain my needs anymore. alt text

The "Fx" is what I need.

avatar image Kiwasi · Nov 05, 2014 at 09:43 PM 0
Share

In which case you do want the cross product between the contact normal and the axel.

It would certainly help to know why you want this force. On a simplified model the forces on the tyre are:

  • A force downwards through the centre via gravity

  • A force upwards through the centre via gravity

  • A torque force applied through the shaft

  • A 'rolling friction force' applied at the contact point between the road and the tyre. This can also be considered as a torque applied in the opposite direction to velocity

All of these forces will be automatically calculated and applied by the physics engine if configured properly. Pay particular attention to friction.

So again my question is - What is your use case? Don't just tell me what force you want with a picture. Tell me why you want it and what game effect you are trying to achieve.

avatar image Daumas18 · Nov 06, 2014 at 07:10 PM 0
Share

I have posted some pictures that show forces in 3D space, maybe these will help you understand my better...

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

6 People are following this question.

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

Related Questions

"Destroy" bullet without turning off trail renderer 2 Answers

Collider2D.GetContacts(List() contacts) works with OnColliderEnter2D but not with OnTriggerEnter2D 1 Answer

Position of a Collision 1 Answer

2D collisions not showing all contact points 0 Answers

How to parent gameobject at point of collision? 0 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