• 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 /
avatar image
1
Question by HJJ · Oct 11, 2011 at 12:41 AM · ai

AI Enemy Scripting

Hi, I'm new to unity and don't know how to script. I'm up to the stage in my game where I need to make enemies. But I only have the mesh and textures. I need it to be able to shoot and follow me, and turn into a rag-doll after 4-5 shots. Any help would be greatly appreciated.

Comment
Add comment · Show 3
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 HJJ · Oct 11, 2011 at 03:13 AM 0
Share

When I finished in monodevelop, the console said:

Assets/RobotAI.cs(1,19): error CS0116: A namespace can only contain types and namespace declarations

if it helps I made it using a C#Script

avatar image matthew_tavares1 · Oct 11, 2011 at 03:36 PM 0
Share

You named your script a different name then the class, in any case change where it says class EnemyAI : $$anonymous$$onoBehaviour { to whatever name your script is.

avatar image HJJ · Oct 12, 2011 at 10:03 PM 0
Share

$$anonymous$$y console is now saying that Gameobject, RaycastHit, Vector3, Rigidbody, Transform and Collision couldn't be found.

I am sorry-I'm useless when it comes to this-What do I do!!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by matthew_tavares1 · Oct 11, 2011 at 01:34 AM

This is a very broad question, specially since you don't have any scripts. You might want to start looking into tutorials. But here is some pretty basic code in C#; using UnityEngine;

class EnemyAI : MonoBehaviour {

 public GameObject Player;

 private float ViewRange = 40;
 private float RayRange = 20;
 private int vel = 8;

 public RaycastHit LastPos;
 public Vector3 RayDirection = Vector3.zero;

 public GameObject target;

 public Rigidbody Bullet;
 public Transform Muzzle;
 
 public void Update()
 {
     RayDirection = Player.transform.position - transform.position;

     if (Vector3.Angle(RayDirection, transform.forward) < ViewRange)
     {
         if (Physics.Raycast(transform.position, RayDirection, out LastPos, RayRange))
         {
             if (LastPos.collider.tag == "Player")
             {
                 Attack();
             }
         }
     }
 }

 public void Attack()
 {
     transform.LookAt(LastPos.transform.position);
     if (RayDirection.magnitude > 10)
     {
         transform.position = Vector3.MoveTowards(transform.position, LastPos.transform.position, Time.deltaTime * vel);
     }

     else
     {
             Rigidbody b = GameObject.Instantiate(Bullet, Muzzle.position, Muzzle.rotation) as Rigidbody;
             b.AddForce(2000 * b.transform.forward);
     }
 }

 private void OnCollisionEnter(Collision Hit)
 {
     if (Hit.gameObject.tag == "Player")
     {
         PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
         ph.AdjustCurrentHealth(-10);
     }
 }

}

Placing that on your enemy will make him able to see and follow the player as well as shoot him. You can mess around with most of the numbered values to get your desired result. I'm sorry I didn't really explain the code, I'm pretty tired right now so I didn't feel like getting into all the details, if you need any more help feel free to comment on here and I'll do my best to help you.

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemy AI monster problem 3 Answers

Trouble with turret script 1 Answer

What is AutoWayPoint 1 Answer

[C#] Raycast based AI 2 Answers

Can you randomise a Navmesh agents rotation 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