0

In Unity I can handle circular motion around an object with a simple

transform.RotateAround(GameObject.Find("CubeTest").transform.position, Vector3.up, 20000*Time.deltaTime);

However, I want the object traveling in circular motion to approach this object whilst in orbit. Not entirely sure how to do this without screwing up.

apxcode
  • 7,696
  • 7
  • 30
  • 41
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • If I'm reading this right you could something like: Vector3 dir = GameObject.Find("CubeTest").transform.position - transform.position; transform.translate(dir.normalized * Time.deltaTime); Just make sure to add a check for when your object is within N units from `CubeTest` – Jerdak Apr 08 '13 at 14:15
  • I'm actually thinking about this one right now as well.. What a coincidence, but I'm interested in this one. I'm not sure how RotateAround works, but wouldn't your solution be completed by using LookAt? – Joetjah Apr 08 '13 at 14:20
  • Oh, quite possibly! Let me take a quick look at it later when I get home. – Stupid.Fat.Cat Apr 08 '13 at 17:26
  • I think that you should also look into iTween to use for such object motions. – Max Yankov Apr 11 '13 at 18:12

2 Answers2

3
GameObject cube = GameObject.Find("CubeTest");    
transform.LookAt(cube.transform);
transform.Translate(transform.forward * Time.deltaTime * approachSpeed);
transform.RotateAround(cube.transform.position, Vector3.up,20000*Time.deltaTime);

I think that could do as you want? It moves towards the rotation point gradually, then rotates, giving the appearance of a deteriorating orbit.

SlxS
  • 424
  • 3
  • 8
  • That what I was thinking as well, though I dare not to speculate about the `RotateAround`-function without me having seen it first. I'm unsure if rotating the object affects the total rotation... – Joetjah Apr 09 '13 at 14:22
0

If you came here looking for working 2D solution here you are.

From this blog post. I built this configurable script:

public class SpiralMovement : MonoBehaviour
{
    [SerializeField] Transform moveable;
    [SerializeField] public Transform destiny;
    [SerializeField] float speed;
    
    // As higher as smoother approach, but it can not be more than 90, or it will start getting away.
    [SerializeField][Range(0f, 89f)] float angle = 60f;

    void Update()
    {
        Vector3 direction = destiny.position - moveable.position;
        direction = Quaternion.Euler(0, 0, angle) * direction;
        float distanceThisFrame = speed * Time.deltaTime;

        moveable.transform.Translate(direction.normalized * distanceThisFrame, Space.World);
    }
}
fguillen
  • 36,125
  • 23
  • 149
  • 210