using UnityEngine; using System.Collections; public class playerFollow : MonoBehaviour { public GameObject target; public float minDistance = 4; public bool active; bool follow = false; private Rigidbody rigidbody; private thirdPersonController playerController; // Use this for initialization void Start () { rigidbody = GetComponent (); playerController = GetComponent (); } // Update is called once per frame void Update () { if (Input.GetButtonDown ("follow")&& !active) follow = !follow; if (Input.GetButtonDown ("playerSwap")) { active = !active; follow = false; } } void FixedUpdate(){ if (follow) followPlayer (); } private void followPlayer(){ Vector3 lookAtTarget = target.transform.position; lookAtTarget.y = transform.position.y; transform.LookAt (lookAtTarget); if (minDistance < Vector3.Distance (target.transform.position, transform.position)) { Vector3 direction = target.transform.position; direction.y = transform.position.y; direction -= transform.position; direction.Normalize (); rigidbody.AddForce (direction * playerController.movementSpeed * Time.deltaTime); } else { playerController.applyGrip (); } } }