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 Josh707 · Oct 28, 2013 at 03:53 AM · physicspositiondragoffsetrigidbody.addforce

Adding a position offset to a force towards an object

So I'm re-creating Amnesia, simply for educational purposes, the only reason I am mentioning this is because of the physics. I don't know how Frictional Games did it exactly, but they achieved a really good looking method of "holding" rigidbodies and having them still collide with others, while being able to rotate and adjust distance at the same time.

What I've made works pretty much the same - When you grab an object, the script saves the initial position and rotation onto 2 other game objects, and a CoRoutine then interpolates the rotation. This lets me smoothly rotate and move the object. In the CoRoutine (while holding), I am calculating a vector that points towards the initial grab point and is scaled by the distance of the two, making it float nicely to the center while still being pushed away/pushing other objects. The concept is very simple:

 //phys_dragger_pos is the initial position - drag_reference is the held rigidbody
 Vector3 vel = ((phys_dragger_pos.position - drag_reference.transform.position).normalized);
 drag_reference.rigidbody.AddForce((vel * (Vector3.Distance(phys_dragger_pos.position, drag_reference.transform.position))), ForceMode.Impulse);

 

But when I grab an object, since I am just adding it to the rigidbody, it's adding the force to the root objects position. So instead of it not moving until I drag it, it brings the base of the object to the initial position. I have tried calculating an offset vector to add to the AddForce function but I'm not very good at math. I have access to the RaycastHit and all information for every object involved. How would I go about adding a position offset to the AddForce function so that it brings the very point I grabbed to the initial position I saved?

Here's a .gif to show what I mean about the base position:

What is this?

The pink thing is just the rotation reference that it interpolates to.

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 robertbu · Oct 28, 2013 at 04:16 AM 0
Share

Have you tried AddForceAtPosition()?

avatar image Josh707 · Oct 28, 2013 at 04:24 AM 0
Share

No, the way I have it working now is how it seems to work in Amnesia. I just need to figure out how to get a Vector3 offset that is basically the distance between the inital point (raycast hit) and the objects initial position, so I can add it to the vel vector making it drag the object from the point that I grabbed it from, ins$$anonymous$$d of the root objects position.

I've tried to make an offset and add it to it but everything I've tried has screwed it up, and it starts shaking for some reason.

avatar image robertbu · Oct 28, 2013 at 04:52 AM 0
Share

Personally I cannot visualize an offset solution like you describe that would solve the problem you describe, but there is a lot I don't know about the physics engine. Typically I see the problem I think you are trying to solve done using hinges. Take a look at the standard DragRigidbody script for example. Another untested idea is to change the center of gravity to the drag point.

avatar image Josh707 · Oct 28, 2013 at 04:56 AM 0
Share

Oh yeah I've spent like an entire day messing with that script... No matter what I tried it always seemed like it was dangling from a single point, and whenever I set the spring strong enough to keep it in place it was shaking and jittering :\

I'm gonna keep trying every combination of the math I can think of until it works, though. Thanks anyways!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RyanZimmerman87 · Oct 28, 2013 at 11:48 PM

Hmm not sure if I'm understanding your problem correctly.

So I'm assuming you are using a RaycastHit to get the position you want to use for the offset like:

rayVector = rayHit.point

So if you have the object position:

pickUpObjectTransform.position;

And the ray position:

rayVector;

You should be able to calculate the distance between those points. That's the calculation you need right? I'm not sure if you have that yet assuming you do.

Seems complicated but I think you might need to create or move a new gameObject at the rayvector position every time you pick something up. Than you could make that object be the parent of the object you pick up. That would allow the picked up object to use transform.localPosition and transform.localRotation so it should maintain the correct position and rotation in relation to the spot you hit with ray.

Hmm I'm having trouble explaining this and I've never tried it personally. But basically I'd try to make your pick up objects temporarily a child of the position your ray hit, and you might want to make the position your ray hit a child of the player?

So hierarchy would be like:

1) Player Object is parent object

2) Player's "hand" is child object to player object

3) The new rayHit.point position is a child to the "hand"

4) the object being picked up is a child to the rayHit.point

That's my best guess at how I would attempt this, but that's just kinda a guess this problem sounds pretty complicated to me.

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 Josh707 · Oct 29, 2013 at 12:12 AM 0
Share

Haha yeah, I literally just got it worked a $$anonymous$$ute ago doing what you said without reading it first. I had a position reference game object parented to my camera, and ins$$anonymous$$d of moving it to the raycast position I moved it to the objects base... I was definitely thinking about it way too hard last night. Anyways, I'll mark your answer as correct and up vote it just because you answered :)

Cheers!

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

16 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

Related Questions

Android Moving with finger without breaking physics 0 Answers

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

How to calculate delta offset time between race cars? 0 Answers

Checking if mouse dragged from one point to another 2 Answers

How to calculate force to apply to move object to a certain distance. 1 Answer


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