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. Dismiss Notice

how to set distance between two objects constant?

Discussion in '2D' started by shophongkong, Apr 29, 2014.

  1. shophongkong

    shophongkong

    Joined:
    Dec 27, 2013
    Posts:
    5
    Hi,

    I'm trying to make one object rotate around another using RotateAround.

    When the rotating object collides with another object, the rotating object goes around the collided object automatically.

    I want the rotating object to have a fixed distance at all times regardless if it collides with another object.

    For example, I have a solid fireball that is twirling around the character, when the ball hits an object. it should stop and not auto try going around it or go through it (keep distance fixed between fireball and character controlling the fireball.

    I have try different combinations of colliders, distance joint and on enter triggers but couldn't the solution. I've been working on this for days.

    How should I go about doing this?
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    That should be a DistantJoint2D, in 2D physics. Why isn't that working for you?
     
  3. shophongkong

    shophongkong

    Joined:
    Dec 27, 2013
    Posts:
    5
    $d_joint.jpg

    I rotate the sphere around the smaller cube, when it hits the larger cube, the sphere goes along the edge of the large cube and eventually goes around it if I keep rotating it. How to make it not go around after hitting the large cube?

    edit:
    Im using transform.RotateAround with distance Joint.

    2nd edit:
    I'm thinking if I'm able to get the distance between the smaller cube and the sphere to be constant, this way it should prevent the sphere from moving around the larger cube. How do I make the sphere not go around the larger cube? More like its blocking the sphere from moving at all.
     
    Last edited: Apr 29, 2014
  4. shophongkong

    shophongkong

    Joined:
    Dec 27, 2013
    Posts:
    5
    I have resolved this by doing a workaround.

    setup:
    distance joint, rigidbody on the object rotating around another object


    1. calculate the distance between two objects using Vector3.Distance...
    2. if the distance is within the desired valued, transform.rotatearound...
    3. else tranform.position = new Vector3(transform.position.x - value... this will keep pushing the object backwords so it cannot wrap around the blocked object.
     
  5. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    FYI.... the DistanceJoint2D currently has a bug in it which causes it to act like a RopeJoint instead. This will be fixed in the next major release.