{"id":2447,"date":"2019-05-25T02:52:38","date_gmt":"2019-05-25T02:52:38","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=2447"},"modified":"2019-05-25T02:55:30","modified_gmt":"2019-05-25T02:55:30","slug":"unity-script-for-enemy-to-follow-player","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/unity-script-for-enemy-to-follow-player\/","title":{"rendered":"Unity script for enemy to follow player"},"content":{"rendered":"\n<p>The following script<em> <\/em>will let an object follow a player when it is within a certain range and will stop following it once it is out of a certain range<\/p>\n\n\n\n<p>The following variables can be adjusted from the Inspector.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"189\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2019\/05\/image.png\" alt=\"\" class=\"wp-image-2448\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2019\/05\/image.png 627w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2019\/05\/image-300x90.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2019\/05\/image-500x151.png 500w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/figure>\n\n\n\n<p>Attack Speed = How fast the game object moves<br>Attack Distance = How close does the player need to be to start moving<br>Buffer Distance = How far away from the player should the game object stop<br>Player = Game player to target<\/p>\n\n\n\n<p>To use script, create a new C# script named FollowPlayer and paste in the following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class FollowPlayer : MonoBehaviour\n{\n\n    public float attackSpeed = 4;\n    public float attackDistance;\n    public float bufferDistance;\n    public GameObject player;\n\n    Transform playerTransform;\n\n    void GetPlayerTransform()\n    {\n        if (player != null)\n        {\n            playerTransform = player.transform;\n        }\n        else\n        {\n            Debug.Log(\"Player not specified in Inspector\");\n        }\n    }\n\n    \/\/ Start is called before the first frame update\n    void Start()\n    {\n        GetPlayerTransform();\n    }\n\n    \/\/ Update is called once per frame\n    void Update()\n    {\n        var distance = Vector3.Distance(playerTransform.position, transform.position);\n        \/\/ Debug.Log(\"Distance to Player\" + distance);\n        if (distance &lt;= attackDistance)\n        {\n            if (distance >= bufferDistance)\n            {\n                transform.position += transform.forward * attackSpeed * Time.deltaTime;\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Helpful Links<\/p>\n\n\n\n<p><a href=\"https:\/\/forum.unity.com\/threads\/c-get-transform-of-another-gameobject.177216\/\">https:\/\/forum.unity.com\/threads\/c-get-transform-of-another-gameobject.177216\/<\/a><br><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Vector3.Distance.html\">https:\/\/docs.unity3d.com\/ScriptReference\/Vector3.Distance.html<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following script will let an object follow a player when it is within a certain range and will stop following it once it is out of a certain range The following variables can be adjusted from the Inspector. Attack &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/unity-script-for-enemy-to-follow-player\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325],"tags":[644,645,606,326,327],"class_list":["post-2447","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-code","tag-player","tag-programming","tag-unity","tag-unity3d"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=2447"}],"version-history":[{"count":2,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2447\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2447\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=2447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=2447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=2447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}