4

I would like to catch an object with another object. And then carry it to the start point of first object, in 2D. I'm throwing an object to another object with a constant velocity. The first one collides with a second one, after that I defined reverse velocity of the first one to the standing second object. When a collision happens (onCollisonListener) it doesn't turn back naturally, so the angle of turning back is wrong.

They go everywhere randomly. How can I make it so that turning back functions properly? Which function could I use? I am using only velocity.x and velocity.y Thanks for reading my question and for your help, Sincerely Yours.

Steven
  • 166,672
  • 24
  • 332
  • 435
OrGor
  • 51
  • 6
  • 1
    If you still need some help, can you post code of your "reverse velocity" calculations? Also screenshot of whats happening would be nice too. – Utamaru May 05 '15 at 13:17
  • You should be more exact in what you need. Do you want the 2nd object to attach fixed (like it's stuck) or more soft (like on a string)? You can re-parent it or use physics links. Also, it is very important if you use Rigidbodies or not. – Tom Jun 23 '15 at 09:47

1 Answers1

0

Pseudo code:

public class Catcher : MonoBehaviour
{
    Vector2 startingPoint;

    void Awake()
    {
        startingPoint = transform.position;
    }

    void Update()
    {
        //Your moving code
    }

    void OnTriggerEnter(Collider other)
    {
         if(other.tag == "someTag")
         {
             other.transform.parent = transform;
             StartCoroutine("MoveBack");
         }
    }

    private IEnumerator MoveBack()
    {
        while(transform.position != startingPoint)
        {
            // Move this towards starting point
            // Use Vector2 functions like Lerp, MoveTowards or SmoothDamp
            // Could also use Mathf functions
            yield null;
        }
    }
}

And maybe add a flag/check to know if the object is moving back or forward.

PsMaster
  • 131
  • 2
  • 10