Created
January 2, 2022 18:48
-
-
Save modyari/1e8ad4576b223c566b40122a73a1bc34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class MoveToPlayer : MonoBehaviour | |
{ | |
public Transform player; | |
public float speed; | |
public float maxDistance; | |
void Update() | |
{ | |
if (Vector3.Distance(player.position, transform.position) < maxDistance) | |
{ | |
float step = speed * Time.deltaTime; | |
transform.position = Vector3.MoveTowards(transform.position, player.position, step); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bad example of how to structure unity behaviours