Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by xIvan95 · Apr 08, 2014 at 04:37 PM · c#aibasic

Help with Basic AI

Hi! I am starting to write AI code and I'm having some issues with a very simple AI.

The issue:

I want the enemy to stop as soon as he enters the maxDistance variable, so he cannot keep going forward. I think my code is right, but it doesn't work and I can't find the problem. Thank you! :D

The code:

using UnityEngine; using System.Collections;

public class BasicAI : MonoBehaviour {

 private GameObject player;
 public float distance;
 private CharacterController thischar;
 public float speed = 5.0f;
 public Vector3 movementh = Vector3.zero;
 public float chaseRange = 1.0f;
 public float attackRange = 1.5f;
 public float gravity = 20.0f;
 public float lookatdistance = 20.0f;
 public float dumping = 8.0f;
 public float maxDistance = 0.4f;

 // Use this for initialization
 void Start () {

     player = GameObject.FindWithTag ("Player");
     thischar = GetComponent <CharacterController>();
 }
 
 // Update is called once per frame
 void Update () {
     distance = Vector3.Distance (player.transform.position, transform.position);
     movementh.y -= gravity * Time.deltaTime;
     thischar.Move (movementh * Time.deltaTime);

     if (distance <= chaseRange)
     {
         if (distance >= maxDistance)
         {
             movementh = transform.forward * speed;
             print ("la distancia es mas grande que la maxima");
         }
         else
         {
             movementh = Vector3.zero;
         }
     

     }
     else
     {

         if (distance <= lookatdistance)
         {
             LookAt ();

         }

     }
 





             





 }

 void LookAt ()
 {
 
         Quaternion rotation = Quaternion.LookRotation (player.transform.position - transform.position);
         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * dumping);
 }



}

Comment
Add comment · Show 4
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 HuskyPanda213 · Apr 08, 2014 at 04:50 PM 0
Share

Your checking: distance >= maxRange, is that what you want, or should it be

avatar image xIvan95 · Apr 08, 2014 at 04:58 PM 0
Share

No, it's okay. Because I want the enemy to go forward as long as he's away from the player, when he gets within the range (float maxDistance), I want him to stop moving.

avatar image BlackPanda · Apr 08, 2014 at 05:22 PM 0
Share

What do you mean not working?? The body is moving at the same speed even when it exceeds the limit??

avatar image xIvan95 · Apr 08, 2014 at 05:28 PM 0
Share

yes........

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by putlucky · Apr 08, 2014 at 07:12 PM

I did something which might help, feel free to add to it, its bareeeebones. You'll need to loop when I disable the script so that it isn't permanently disabled, but otherwise it could be helpful.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public GameObject target; 
     public bool foundPlayer; 
     public Vector3 myPosition;
     public float speed = 1.0f; 
 
     public EnemyAI myAI; 
 
 
     void Start () {
     // Locate the Player object. 
         if(GameObject.FindWithTag("Player"))
         {
             myAI = GetComponent<EnemyAI>(); 
             foundPlayer = true;
         }
 
     }
 
     void Attack() 
     {
         myPosition = transform.position;
         transform.position = Vector3.Slerp(myPosition, target.transform.position, speed * Time.deltaTime);
 
 
     }
 
     void Update () {
         if(foundPlayer == true) 
         {
             Attack(); 
 
             if(Vector3.Distance(myPosition, target.transform.position)<2.0f)
             {
                 myAI.enabled = false;
             } else
             {
                 myAI.enabled = true;
             }
         }
 
 
     }
 }
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.

Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image avatar image

Related Questions

Basic AI Movement C# 3 Answers

What are IEnumerator and Coroutine? 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Enemy pick and throw an object 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