Simple 2D AI Chase Script for Unity 3D in C#

&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;92 -->&NewLine;<div class&equals;"quads-location quads-ad1" id&equals;"quads-ad1" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;<p>Simple game AI can be hard to figure out in Unity 3D without add-ons&period; Fortunately&comma; you can use a simple C&num; script to have an enemy chase a player&period; Make sure your Player object has the &&num;8220&semi;Player&&num;8221&semi; tag set&period; Then add this script to your enemy object&comma; and set the &&num;8220&semi;Move Speed&&num;8221&semi; and &&num;8220&semi;Rotation&&num;8221&semi; speed to values that work for your game&period; Make sure you have sprites set for both your player and enemy game objects&period; Then run the game&period; Here is the script&colon;<&sol;p>&NewLine;<blockquote><p>using UnityEngine&semi;<&sol;p>&NewLine;<p>using System&period;Collections&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>public class EnemyAI &colon; MonoBehaviour &lbrace;<&sol;p>&NewLine;<p>public Transform target&semi;<&sol;p>&NewLine;<p>public int moveSpeed&semi;<&sol;p>&NewLine;<p>public int rotationSpeed&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>private Transform myTransform&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&sol;&sol; Use this for initialization<&sol;p>&NewLine;<p>void Awake&lpar;&rpar; &lbrace;<&sol;p>&NewLine;<p>myTransform &equals; transform&semi;<&sol;p>&NewLine;<p>&rcub;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>void Start &lpar;&rpar; &lbrace;<&sol;p>&NewLine;<p>GameObject go &equals; GameObject&period;FindGameObjectWithTag&lpar;&&num;8220&semi;Player&&num;8221&semi;&rpar;&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>target &equals; go&period;transform&semi;<&sol;p>&NewLine;<p>&rcub;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&sol;&sol; Update is called once per frame<&sol;p>&NewLine;<p>void Update &lpar;&rpar; &lbrace;<&sol;p>&NewLine;<p>Vector3 dir &equals; target&period;position &&num;8211&semi; myTransform&period;position&semi;<&sol;p>&NewLine;<p>dir&period;z &equals; 0&period;0f&semi; &sol;&sol; Only needed if objects don&&num;8217&semi;t share &&num;8216&semi;z&&num;8217&semi; value<&sol;p>&NewLine;<p>if &lpar;dir &excl;&equals; Vector3&period;zero&rpar; &lbrace;<&sol;p>&NewLine;<p>myTransform&period;rotation &equals; Quaternion&period;Slerp &lpar;myTransform&period;rotation&comma;<&sol;p>&NewLine;<p>Quaternion&period;FromToRotation&lpar;Vector3&period;right&comma; dir&rpar;&comma; rotationSpeed &ast; Time&period;deltaTime&rpar;&semi;<&sol;p>&NewLine;<p>&rcub;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&sol;&sol;Move Towards Target<&sol;p>&NewLine;<p>myTransform&period;position &plus;&equals; &lpar;target&period;position &&num;8211&semi; myTransform&period;position&rpar;&period;normalized &ast; moveSpeed &ast; Time&period;deltaTime&semi;&rcub;<&sol;p>&NewLine;<p>&rcub;<&sol;p><&sol;blockquote>&NewLine;<p>Let us know how it works for you or if you have a better script&comma; in the comments below&excl;&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;92 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;<&sol;p>&NewLine;<p><em>Original script by <a href&equals;"https&colon;&sol;&sol;answers&period;unity&period;com&sol;users&sol;95255&sol;penfwasaurus&period;html">penfwasaurus<&sol;a>&period; Updated by <a href&equals;"https&colon;&sol;&sol;answers&period;unity&period;com&sol;users&sol;16320&sol;robertbu&period;html">robertbu<&sol;a>&period; Compiled&comma; tested and used by <a href&equals;"http&colon;&sol;&sol;marketinghackers&period;net&sol;about&sol;">Marketing Hackers<&sol;a>&period; Originally published at <a href&equals;"https&colon;&sol;&sol;answers&period;unity&period;com&sol;questions&sol;604858&sol;simple-ai-in-2d-c&period;html">Unity Answers<&sol;a>&period;<&sol;em>&NewLine;

Mont has 10+ years experience in digital marketing. Experienced with LAMP, CMS and HTML5/CSS3 website development, Mont also excels at math and C/C++/C#. He has managed multi-million dollar media buys though Outbrain, Facebook, Taboola, Yahoo Native, Google AdWords and a number of other display advertising vendors. Mont's written work has regularly appeared on the first page of Google News results and the homepage of Yahoo! He has a B.Sc. in Business Administration with a concentration in Marketing from Drexel University.
Mont Cessna

Mont has 10+ years experience in digital marketing. Experienced with LAMP, CMS and HTML5/CSS3 website development, Mont also excels at math and C/C++/C#. He has managed multi-million dollar media buys though Outbrain, Facebook, Taboola, Yahoo Native, Google AdWords and a number of other display advertising vendors. Mont's written work has regularly appeared on the first page of Google News results and the homepage of Yahoo! He has a B.Sc. in Business Administration with a concentration in Marketing from Drexel University.

Leave a Comment
Share
Published by
Mont Cessna

Recent Posts

The Top 5 Facebook Advertising Mistakes I See Clients Make

I spend a lot of money on Facebook advertising for both businesses and other marketing…

7 years ago

The Top 5 Mistakes I See Digital Marketing Clients Make

I've worked as a digital marketing consultant for dozens of businesses across different industries and…

7 years ago

Interview of F5Bot Founder Lewis Van Winkle

F5Bot is a free service that emails you when your selected keywords are mentioned on Reddit, Hacker…

8 years ago

How Marketing Hackers Turbocharge Growth Hacking

Marketing Hackers is a new media and digital marketing consulting company that focuses on growth…

8 years ago

Promising Opportunities for Marketers Using Crytocurrency

Marketers have often leveraged the internet's large platforms like social media for marketing. Nevertheless, increasing…

8 years ago

Blockchain Technology Is Disrupting Traditional Digital Advertising

In Laymen's terms, Blockchain Technology can be explained as spreadsheets or ledgers which are shared…

8 years ago