• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 29, 2014 at 05:43 AM by robertbu for the following reason:

Multiple technical questions

avatar image
-1
Question by kiwiboys · Apr 29, 2014 at 05:08 AM · c#ai

Getting a 2d enemy (sprite) to look at & attack the player

Ok so lately i have been putting the Ai off but now i have to do this before friday to get the next Dev Build out for testing. so what i need to do is create The Basic Ai i already have a basic vision for the enemy and now what i need to do is to get the enemy to face in the direction of the player and then shoot at the player but soot only every 2 seconds for example. this is what i have so far (enemy movement is on a differentscript i can disable that when the player is in sight)

 using UnityEngine;
 using System.Collections;
 
 public class EnemeyActuall_AI : MonoBehaviour {
 
 
 
 
     public bool Playerinsight = false;
 
 
 
 
 
 
 
     // Use this for initialization
     void Start () 
     {
   
         Playerinsight = false;
     }
     
     // Update is called once per frame
 
 
     void Update () 
     {
         if (Playerinsight == true)
         {
             transform.LookAt(Player);//Not working???
 
         }
 
         else if (Playerinsight == false)
         {
 
 
 
         }
     }
 
 
 
     void OnTriggerEnter2D (Collider2D col)
     {
         // If it hits an enemy...
         if (col.tag == "Player") {
             // ... find the Enemy script and call the Hurt function.
             {
                 Playerinsight = true;
                 print ("you have been seen");
             }
         
         }
 
     }
 
 
 
 }
 

Thanks Any help would be very much appreciated.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Apr 29, 2014 at 05:20 AM 0
Share

You don't show a definition of "Player". Use a public var drag/drop or GetComponent to define Player, then change it to Player.transform.position lookat wants a Vector3

Shoot and timers are covered in many examples.

avatar image robertbu · Apr 29, 2014 at 05:42 AM 1
Share

You have multiple technical problems in this question: 1) look at player, 2) shoot at the player, 3) shoot at some interval. We ask that each Unity Answer question focus on a single issue. In addition #2 and #3 have been covered 1000s of times on this list. Assu$$anonymous$$g 2D, the 'LookAt' for 2D has been covered a several dozen since the new 2D features were added. Here is some sample code:

 Vector3 dir = player.position - transform.position;
 float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Please search out solutions for #2 and #3. If you get stuck, post back with your attempted solution and what is wrong.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

EnemyAI C# Error 1 Answer

AI Look at X axis more efficient code 1 Answer

Need Help making Pong AI beatable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges