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 meat5000 · Jul 25, 2013 at 10:09 AM · movementrigidbodydirectionforcevector

How to fix/clamp rigidbody dir vector, despite Force dir

I wish to bind a rigidbody to move only along a fixed vector between two points, despite any forces that may be acting on it.

I understand in Maths how to apply the x,y components of a Force to find the resultant on a mass in its direction but I can't find a way to bind the rigidbody to the fixed vector and prevent it from veering off course, but still respond to the components of physics in its direction.

Imagine a cube with a hole sliding along a pole fixed in space. You can apply a force in any direction you like but the pole fixes movement along one vector and so only the force components in the direction of the vector will produce a movement.

Creating that imaginary pole and fixing the vector is what I'm having the problem with. I require non-K rigidbody so the cube is influenced by a changing gravity.

Edit:

(Could I achieve this with var Properties?)

Comment
Add comment · Show 10
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 Seth-Bergman · Jul 25, 2013 at 10:19 AM 1
Share

basically, just use AddRelativeForce

if it's fixed to the "pole" though, you probably could fake the changing gravity very easily either way...

it may help to freeze rotation too..

EDIT:

as in:

 rigidbody.AddRelativeForce(amount,0,0);

How I would do it:

don't allow any actual collision to the cube, just child an empty object with a box collider to it, and put the main box on a separate collision layer, to serve as a collider for the impact detection.. and child the box to the pole. that should work I think.

avatar image meat5000 ♦ Seth-Bergman · Jul 25, 2013 at 10:51 AM 0
Share

Hi Seth, thanks for your swift reply! I will try the parenting and layering and see how it works for me.

I am currently using :

 rigid.AddRelativeForce(Vector3.up*resultForce.magnitude*gravDot);

cube is spawned in direction that Vector3.up follows the vector.

where :

 gravDot = Vector3.Dot(startVector.normalized, Physics.gravity.normalized);
 
 gravX = Physics.gravity.x * gravDot;
 gravY = Physics.gravity.y * gravDot; ETC
 resultForce = Vector3(gravX,gravY,gravZ);

I have frozen all rotations and z plane.

The rigidbody behaves well under the gravity, however when it collides with something else (a floor), the physics engine makes it move in a resultant in x,y and veer from the vector. The biggest culprit is the bounce factor, which I can't seem to eli$$anonymous$$ate without messing up my cube gravity response.

I am trying to eli$$anonymous$$ate this veering and ins$$anonymous$$d calculate the component influence within the vector of whichever force causes the veering. Hope I make sense!

avatar image Hyperion Seth-Bergman · Aug 31, 2013 at 01:50 AM 1
Share

If you're still having issues with bounciness, you need to go into the physics manager (open the physics material on your cube) and change the 'bounciness' to 0.

avatar image Hyperion Seth-Bergman · Sep 01, 2013 at 02:39 PM 1
Share

I believe the bounce is the percent of how much of its original height it will bounce again, so .75 would slowly bounce less and less.

Show more comments
avatar image Hyperion · Sep 02, 2013 at 09:13 PM 1
Share

Have you tried using Clamp$$anonymous$$agnitude? http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Clamp$$anonymous$$agnitude.html

1 Reply

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

Answer by meat5000 · Sep 03, 2013 at 12:24 AM

So this worked for me. This script is on a non-K rigidbody with collider and 'Use Gravity' OFF. I am changing Physics.Gravity with another script but this object only moves in its local Up/Down direction under the influence of the component of Gravity that is acting in that direction Vector. When the object lands on its cylindrical floor or concave ceiling it normally tends to bounce and slide off vector but this script calculates the displacement in Local X axis and adds a force in World Space to correct the displacement.

Blocks are now all aligning wonderfully in my game. I know this can be condensed but it really helps to see what you can do. (Plus tried a load of stuff that just didn't work). Also, it didn't touch the fps even with loads of objects.

 //
 #pragma strict
 var ImpulseCollisionMax : float = 10.0;
 var snapFactor : float = 2.0;
 
 var startVector : Vector3;
 var spawnAngle : float = 0.0;
 private var rigid : Rigidbody;
     private var gravDot : float = 0.0;
     private var gravX : float = 0.0;
     private var gravY : float = 0.0;
     private var gravZ : float = 0.0;
     private var resultForce : Vector3;
 private var startTrans : Transform;
 private var startLocalPos : Vector3;
 private var localPosition : Vector3;
 private var blockRelativeXDiff : float = 0.0;
 private var blockDiffVector : Vector3;
 
 function Start ()
 {
     startTrans = transform;
     startLocalPos = transform.InverseTransformDirection(startTrans.position);
     rigid.useConeFriction = true;
     startVector = Vector3(transform.position.x, transform.position.y, 0);
     spawnAngle = Mathf.Atan2(startVector.x, startVector.y) * Mathf.Rad2Deg;
 }
 
 function FixedUpdate ()
 {
     localPosition = transform.InverseTransformDirection(transform.position);
     
     blockRelativeXDiff = localPosition.x - startLocalPos.x;
     blockDiffVector = startTrans.TransformDirection (blockRelativeXDiff, 0, 0);
     
     if (blockRelativeXDiff != 0)
     {
         rigid.AddForce(-blockDiffVector*snapFactor);
     }
     gravDot = Vector3.Dot(startVector.normalized, Physics.gravity.normalized);
         gravX = Physics.gravity.x * gravDot;
         gravY = Physics.gravity.y * gravDot;
         gravZ = 0;//Physics.gravity.z * gravDot;
     resultForce = Vector3(gravX,gravY,gravZ);
     
     rigid.AddRelativeForce (Vector3.up * resultForce.magnitude * gravDot, ForceMode.Acceleration);    
 }
 function OnCollisionEnter(Land : Collision)
 {
     rigid.velocity = Vector3(0,0,0);
 }
Comment
Add comment · Show 4 · 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 Hyperion · Sep 03, 2013 at 12:28 AM 0
Share

I'm sorry I couldn't help much. Looking at the code, it seems you know some complex math, eh?

avatar image meat5000 ♦ · Sep 03, 2013 at 12:38 AM 0
Share

Take a look through :) It's not that bad.

avatar image Hyperion · Sep 03, 2013 at 12:41 AM 0
Share

Yeah, I looked through. I still couldn't quite understand your game mechanics for some reason. I mean the reason to restrain the movement in such a way. But that's probably because it's really original.

avatar image Hyperion · Sep 03, 2013 at 12:42 AM 0
Share

I have a new question, by the way, would you be kind enough to check it out?

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

17 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

Related Questions

Change rigidbody direction 1 Answer

Kinematics: Trying to get an object to land at a known point 1 Answer

[Solved]Why doesn't this Rigidbody.AddForce work the way I tell it to? 1 Answer

Rigidbody movement 1 Answer

Gameobject is climbing out objects with rigidbody 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