You can use lerp to always go to the object, no matter where it is. For example:
Create a cube
Create a Ball
Assign this script to the ball:
using UnityEngine;
using System.Collections;
public class BallBehaviour : MonoBehaviour {
public GameObject Cube;
// Use this for initialization
void Start () {
StartCoroutine(FollowCube());
}
IEnumerator FollowCube()
{
while(true)
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, Cube.transform.position, 0.1f);
yield return null;
}
}
}
Assign the cube to the "Cube" variable of the script
Now you can run your game and the ball will always move towards the cube, no matter where the cube is (you can move it in the editor view to test)
Obviously, if you don't move the target object the trajectory will be straight, which is what you need.
↧