Here we explain how to make a ball follow the player by adding forces to the ball movement and with a navigation through a NavMesh to negociate obstacles. We create this script in the ball Inspector and also we add a NavMeshAgent component to the ball:
using UnityEngine;using System.Collections;public class MoveTo : MonoBehaviour { Transform player; NavMeshAgent nav; NavMeshPath navPath; Vector3 direction; void Awake () { player = GameObject.FindGameObjectWithTag ("Player").transform; nav = GetComponent <NavMeshAgent> (); nav.Stop (true); navPath = new NavMeshPath (); } void Update () { nav.CalculatePath (player.position, navPath); int i = 1; while (i < navPath.corners.Length) { if (Vector3.Distance (transform.position, navPath.corners [i]) > 0.5f) { direction = navPath.corners [i] - transform.position; break; } i++; } } void FixedUpdate() { rigidbody.AddForce (direction.normalized * 10); }}Drag the following script in the inspector of the follower GameObject:
using UnityEngine;using System.Collections;public class MoveTo : MonoBehaviour { public float speed = 5.0F; GameObject player; Rigidbody rb; Vector3 movement; NavMeshAgent follower; void Start () { player = GameObject.Find ("Player"); rb = GetComponent<Rigidbody> (); follower = GetComponent<NavMeshAgent>(); } void FixedUpdate() { Pursuit (); } void Pursuit() { follower.destination = player.transform.position; transform.LookAt (follower.destination); rb.AddRelativeForce (Vector3.forward.normalized * speed); }}For this script to work, you must build first a Navigation Mesh and create a NavMesh Agent for the ball.