You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

64 lines
1.2 KiB

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<Rigidbody> ();
playerController = GetComponent<thirdPersonController> ();
}
// 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 ();
}
}
}