Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

ai code don't work

Discussion in 'Scripting' started by thefreshcoder21, Jun 9, 2020.

  1. thefreshcoder21

    thefreshcoder21

    Joined:
    Mar 13, 2020
    Posts:
    6
    the way the game works i can't use unity's ai system so i tried to make my own but it isn't working
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class enemyMove : MonoBehaviour
    6. { //variables
    7.     private bool doMove = false;
    8.     public float notmove = -1f;
    9.     public float movespeed = 1f;
    10.  
    11.     private Quaternion moveDir;
    12.     // Update is called once per frame
    13.     void Update()
    14.     {   //gets the direction
    15.         moveDir = GameObject.Find("direction").GetComponent<playerDir>().dir;
    16.  
    17.         if(doMove == false && Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))
    18.         {
    19.             doMove = true;
    20.          
    21.         }
    22.         Debug.Log(moveDir.z);
    23.  
    24.         StartCoroutine(MoveCoroutine());
    25.     }
    26.     IEnumerator MoveCoroutine()
    27.     {
    28.         if (doMove)
    29.         {
    30.          
    31.             if (moveDir.z < 0.3f && moveDir.z > -0.3f)
    32.             {
    33.                 for (int i = 0; i < 10; i++)
    34.                 {
    35.                     this.transform.Translate(Vector2.right * movespeed);
    36.                     yield return new WaitForSeconds(0.01f);
    37.                 }
    38.                 yield return new WaitForSeconds(0.01f);
    39.                 fixPos();
    40.                 doMove = false;
    41.                 Debug.Log("right");
    42.             }
    43.             if (moveDir.z < 0.9f && moveDir.z > 0.3f)
    44.             {
    45.                 for (int i = 0; i < 10; i++)
    46.                 {
    47.                     this.transform.Translate(Vector2.up * movespeed);
    48.                     yield return new WaitForSeconds(0.01f);
    49.                 }
    50.                 yield return new WaitForSeconds(0.01f);
    51.                 fixPos();
    52.                 doMove = false;
    53.                 Debug.Log("up");
    54.             }
    55.             if (moveDir.z < -0.3f && moveDir.z > -0.9f)
    56.             {
    57.                 for (int i = 0; i < 10; i++)
    58.                 {
    59.                     this.transform.Translate(Vector2.up * notmove);
    60.                     yield return new WaitForSeconds(0.01f);
    61.                 }
    62.                 yield return new WaitForSeconds(0.01f);
    63.                 fixPos();
    64.                 doMove = false;
    65.                 Debug.Log("down");
    66.             }
    67.             if (moveDir.z < 0.9f && moveDir.z > -0.9f)
    68.             {
    69.                 for (int i = 0; i < 10; i++)
    70.                 {
    71.                     this.transform.Translate(Vector2.right * notmove);
    72.                     yield return new WaitForSeconds(0.01f);
    73.                 }
    74.                 yield return new WaitForSeconds(0.01f);
    75.                 fixPos();
    76.                 doMove = false;
    77.                 Debug.Log("left");
    78.             }
    79.         }
    80.     }
    81.     Vector2 position;
    82.     Vector2 NewPosition;
    83.     void fixPos()
    84.     {
    85.         position = this.transform.position;
    86.         position = position / 20f;
    87.         position.y = Mathf.Round(position.y);
    88.         position.x = Mathf.Round(position.x);
    89.         NewPosition = position;
    90.         this.transform.position = NewPosition * 20f;
    91.     }
    92. }
    93.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    "Don't work" doesn't help anyone to help you.
    • What are you trying to accomplish?
    • What is happening instead?
    • If there's an error message, what's the exact error message (with all the numbers etc. copied)
     
  3. thefreshcoder21

    thefreshcoder21

    Joined:
    Mar 13, 2020
    Posts:
    6
    the ai move 1 tile based on were you are on the board but for some reason it dosn't move left
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Why are you mucking around in the components of a Quaternion to figure out which direction to move? There's something really fishy with that. What does the "playerDir" script look like?
     
  5. thefreshcoder21

    thefreshcoder21

    Joined:
    Mar 13, 2020
    Posts:
    6
    playerDir code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerDir : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public Quaternion dir;
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         //What is the difference in position?
    13.         Vector3 diff = (player.transform.position - transform.position);
    14.  
    15.         //We use aTan2 since it handles negative numbers and division by zero errors.
    16.         float angle = Mathf.Atan2(diff.y, diff.x);
    17.  
    18.         //Now we set our new rotation.
    19.         transform.rotation = Quaternion.Euler(0f, 0f, angle * Mathf.Rad2Deg);
    20.         dir = transform.rotation;
    21.        // Debug.Log(dir);
    22.     }
    23. }
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You shouldn't look directly at the Quaternion Z value. You'll want either transform.eulerAngles or moveDir.eulerAngles. Then look at the Z value.
     
  7. thefreshcoder21

    thefreshcoder21

    Joined:
    Mar 13, 2020
    Posts:
    6