how to follow the player unity code example
Example 1: how to make an object follow the player in unity
Add this to the zombie(s):
//You may consider adding a rigid body to the zombie for accurate physics simulation
private GameObject wayPoint;
private Vector3 wayPointPos;
//This will be the zombie's speed. Adjust as necessary.
private float speed = 6.0f;
void Start ()
{
//At the start of the game, the zombies will find the gameobject called wayPoint.
wayPoint = GameObject.Find("wayPoint");
}
void Update ()
{
wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
//Here, the zombie's will follow the waypoint.
transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
}
//Add this to the player
//In the editor, add your wayPoint gameobject to the script.
public GameObject wayPoint;
//This is how often your waypoint's position will update to the player's position
private float timer = 0.5f;
void Update ()
{
if(timer > 0)
{
timer -= Time.deltaTime;
}
if(timer <= 0)
{
//The position of the waypoint will update to the player's position
UpdatePosition();
timer = 0.5f;
}
}
void UpdatePosition()
{
//The wayPoint's position will now be the player's current position.
wayPoint.transform.position = transform.position;
//Now, create an empty gameobject and make its position equal to the player's position, however, do NOT parent it. Name the empty gameobject "wayPoint". Within the editor, add the waypoint to the player. Be aware that this is a VERY basic script.
}
Example 2: how to make a follow script in unity
var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform; //current transform data of this enemy function Awake() { myTransform = transform; //cache transform data for easy access/preformance } function Start() { target = GameObject.FindWithTag("Player").transform; //target the player } function Update () { //rotate to look at the player var distance = Vector3.Distance(myTransform.position, target.position); if (distance<=range2 && distance>=range){ myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); } else if(distance<=range && distance>stop){ //move towards the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; } else if (distance<=stop) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); } }
Example 3: how to make a follow script in unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public float attackSpeed = 4;
public float attackDistance;
public float bufferDistance;
public GameObject player;
Transform playerTransform;
void GetPlayerTransform()
{
if (player != null)
{
playerTransform = player.transform;
}
else
{
Debug.Log("Player not specified in Inspector");
}
}
// Start is called before the first frame update
void Start()
{
GetPlayerTransform();
}
// Update is called once per frame
void Update()
{
var distance = Vector3.Distance(playerTransform.position, transform.position);
// Debug.Log("Distance to Player" + distance);
if (distance <= attackDistance)
{
if (distance >= bufferDistance)
{
transform.position += transform.forward * attackSpeed * Time.deltaTime;
}
}
}
}