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 Z171 · Aug 30, 2014 at 06:49 PM · aibot

bot AI half working

hi all, i've made this simple script for a walking bot, but i don't know why, sometimes it simply crashes and the gameobject stops moving or doing anything.

here's the script using UnityEngine; using System.Collections;

 public class EnemyMoving : MonoBehaviour {
 
     public GameObject player;
     public int temp;
     public float turnTemp;
     public float distanceTemp;
     public Vector3 translation;
     public GameObject target; 
     public float distance;
     public float maxDistance = 20f;
     public float minDistance = 5.0f;
     private bool isWalking = false;
     private bool isRotating = false;
     private float startPointX;
     private float startPointY;
     private float startPointZ;
     private float time0;
     public float waitTime;
     private bool firstTimeWait = true;
     public Quaternion targetRotation;
     public Vector3 tempVector3;
     private enum states
     {
         NONE,
         WAIT,
         TURN,
         AHEAD
     }
     private states currentState;
 
     void Start () 
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }
     
 
     void Update () 
     {
         if (currentState == states.AHEAD)
         {
             GoAhead();
         }
         else if (currentState == states.TURN)
         {
             turn();
         }
         else if (currentState == states.WAIT)
         {
             Wait();
         }
         else if (currentState == states.NONE)
         {
             FirstLevelRandom();
         }
     }
 
     void FirstLevelRandom()
     {
         temp = Random.Range(0,3);
         switch(temp)
         {
         case 1:
             currentState = states.WAIT;
             Debug.Log("wait");
             break;
         case 2:
             currentState = states.TURN;
             Debug.Log("turn");
             break;
         case 3:
             currentState = states.AHEAD;
             Debug.Log("ahead");
             break;
         }
     }
 
     void Wait() 
     {
         if (firstTimeWait)
         {
             waitTime = Random.Range(2,8);
             firstTimeWait = false;
             time0 = Time.time;
         }
         else
         {
             float time1 = Time.time;
             if (time1 - time0 >= waitTime)
             {
                 currentState = states.NONE;
                 firstTimeWait = true;
             }
         }
     }
 
     void turn()
     {
         if (!isRotating)
         {
             turnTemp = Random.Range(0, 360);
             isRotating = true;
             targetRotation = Quaternion.Euler(0, turnTemp, 0);
         }
         else 
         {
             if (transform.rotation.y != targetRotation.y)
             {
                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 100f);
             }
             else 
             {
                 currentState = states.NONE;
                 isRotating = false;
             }
         }
     }
 
     void GoAhead()
     {
         if (!isWalking)
         {
             startPointX = transform.position.x;
             startPointY = transform.position.y;
             startPointZ = transform.position.z;
             distanceTemp = Random.Range(10, 50);
             tempVector3 = new Vector3 (startPointX, startPointY, startPointZ + distanceTemp); 
         }
         else
         {
             if (transform.position != tempVector3)
             {
                 transform.Translate(Vector3.forward * Time.deltaTime * 10);
             }
             else
             {
                 currentState = states.NONE;
                 isWalking = false;
             }
         }
     }
 
     void FollowPlayer()
     {
         transform.LookAt(player.transform);
         transform.Translate(Vector3.forward * Time.deltaTime);
     }
 }
 

i know that the code is not optimized at all, can anyone find the bug here?? thank you very much!!

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 Z171 · Aug 30, 2014 at 07:30 PM 0
Share

no, simply anything happens, i've tried it sometimes with debug logs, it passed from one function to another, but at some point, it stopped working, the game was fine, but the AI script stopped changing state, also the random function selector was stopped, like if it was in a state and the function was not complete. this has happened three or four times. besides sometimes when it go into the "goAhead" function, never stops going.

avatar image Z171 · Aug 30, 2014 at 07:46 PM 0
Share

after more debug, i've fixed the "freeze" code, it was the last else condition in the turn function and the missing isWalking = true in the first condition of the goAhead function. But now, the gameobject goes ahead until i stop the game, and it turns randomly in the goAhead function

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

make an AI Flee 0 Answers

Simple AI Aircraft Script 2 Answers

ai script help. please help this script. 1 Answer

AI fleeing from multiple enemies 0 Answers

Audio Source AI Attractor 2 Answers


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