Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

moving a pivot point during runtime

Discussion in 'Scripting' started by ThatHomelessGuy, Jun 28, 2010.

  1. ThatHomelessGuy

    ThatHomelessGuy

    Joined:
    Jun 28, 2010
    Posts:
    348
    My problem here is that i cant get a platform to tilt or rotate based on the position of a ball that is constantly moving across the surface of the platform.

    I need to constantly update the position of the platforms pivot point to that of the balls pivot point. I cannot find a script to do this, has anybody got any ideas???

    this is my first post and my last resort lol

    also i am working with java
     
  2. ThatHomelessGuy

    ThatHomelessGuy

    Joined:
    Jun 28, 2010
    Posts:
    348
    I've tried writing script to do this and nothing seems to work.

    Can nobody even point me in the right direction or at least help me adapt this:

    http://solvethesystem.wordpress.com/setpivot/

    to suit my needs.
    the problem is truely frustrating and causes bounce when the platform tilts and the ball cant keep up and creates microbounce on some occasions when the ball lands.
    microbounce causes the ball to pass through the platform.
    the aim is to minimise bounce and to make it feel more natural to the player as if they are holding the platform in their hands. i have the tild controlls down i just need help implementing the moving pivot system at runtime.

    Please?
     
  3. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Pivot(Empty GameObject)
    -> Platform(Mesh)

    Make the Pivot track the position of the ball.

    Make the platform move in the negative position of the ball (or lock it's position to 0)

    Parent the Platform to the Pivot.

    This should allow you to move the pivot, however it may cause unusual behavior if the pivot is constantly moving while already rotated.
     
  4. ThatHomelessGuy

    ThatHomelessGuy

    Joined:
    Jun 28, 2010
    Posts:
    348
    thank's i'll try that.
    the unusual behavior due to rotation has been solved by confining the rotation to the local and the tilt to the global and fixing the camera to a global angle/rotation as it folows the ball.
    thus the platform always tilts relative to the camera regardless of the rotation
    simple engineering :D
     
  5. ThatHomelessGuy

    ThatHomelessGuy

    Joined:
    Jun 28, 2010
    Posts:
    348
    ok so the theory is sound but i'm getting something wrong
    i get no errors but the platform still moves on its own axis even though i aplied the tilting script directly to the Empty GameObject.

    the game object does follow the ball 8)
    the table moves in the oposite direction as the ball too but doesn't stay where it is in world space.

    i tried to make the script as simple as possible so here it is

    i'm probably doing somethig drasticly wrong here

    Code (csharp):
    1. var target : Transform;
    2. var table : Transform;
    3.  
    4. function LateUpdate () {
    5.     if (target)
    6.     {
    7.         transform.position = target.position;
    8.         table.transform.position = -target.position;
    9.        
    10.     }
    11. }
    the target var is the ball and the table var is the platform.
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Ahh, use .localPosition, or set position to Vector3.zero(or whatever it was at the start of the game)

    Although thinking about this more I may be going about this wrong as well. It may roll around the ball quite a bit, I'll have to check in a bit to double check that would get you what you need.

    Edit: This would work on a model, but because Unity always seems to centre to mass, it doesn't seem to work correctly for Unity.
     
  7. ThatHomelessGuy

    ThatHomelessGuy

    Joined:
    Jun 28, 2010
    Posts:
    348
  8. Baalhug

    Baalhug

    Joined:
    Aug 12, 2013
    Posts:
    32
    I'm in this same problem. Though 9 years passed since last update, I'll comment my experience:
    1.- If you use transform.position and transform.rotation, physics collision will only work on discrete mode, so the ball can pass through platform's mesh. If you use this physics will try to fix any collision every frame once the platform has been relocated or rotated. It's recommended to use Rigidbody.MoveRotation to properly calculate collisions between frames.
    2.- The script shown in http://solvethesystem.wordpress.com/setpivot/ is usefull to relocate the pivot point of a mesh once (at start, for example) but not to update each frame, as you want to do. It's insane to relocate every vertex of the mesh each frame.
    3.- Parenting the platform to an empty GameObject (which acts as the pivot) is the less problematic aproach of the "usual ones", but as Ntero said, you must attach the pivot to the ball so it keeps track of the ball movement and move the platform locally to its parent (the pivot point) in the opposite direction to avoid the platform follows the ball as well and remains in the same global position. Now to do this you may not use transform.position but Rigidbody.MovePosition instead and this may create additional collisions while moving the platform. Also I don't know any way of using MovePosition locally, so I think this solution may only work with transform.position and rotation.
    4.- The best solution (by far) I have found is explained in this post: https://answers.unity.com/questions/10093/rigidbody-rotating-around-a-point-instead-on-self.html by Sandy Gifford. Tested and works like a charm.