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 Movement Problems

Discussion in 'Scripting' started by Fynn_GER, Jun 30, 2020.

  1. Fynn_GER

    Fynn_GER

    Joined:
    Jun 30, 2020
    Posts:
    9
    Hello!
    I am trying to script a very simple Ai for my game.
    This Code is just for moving my Ai in four DIrections which where randomly chosen.
    So very basic.
    But when I try this script on a gameobject in Unity it just flows away and ist not working at all.
    I set many times that the Y Position cant be changed but the script gives a S***.
    I dont know what do to any more so I would apreciate some help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ai : MonoBehaviour
    6. {
    7.     float direction;
    8.     float positionx;
    9.     float positiony;
    10.     float positionz;
    11.  
    12.     public float timeLeft = 4.0f;
    13.  
    14.     Vector3 moveDirection;
    15.  
    16.     public Transform enemy;
    17.  
    18.     void Start()
    19.     {
    20.         direction = Random.value;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         timeLeft -= Time.deltaTime;
    26.  
    27.         CharacterController controller = GetComponent<CharacterController>();
    28.  
    29.         positionx = (enemy.transform.position.x);
    30.         positiony = (2.77f);
    31.         positionz = (enemy.transform.position.z);
    32.  
    33.         if(direction < 0.25f)
    34.         {
    35.             while(timeLeft > 0)
    36.             {
    37.                 transform.Rotate(0, 180, 0);
    38.                 moveDirection = new Vector3(positionx, 2.77f, 0.25f);
    39.                 controller.Move(moveDirection * Time.deltaTime);
    40.                 break;
    41.             }
    42.             if (timeLeft < 0)
    43.             {
    44.                 direction = Random.value;
    45.                 timeLeft = 4.0f;
    46.             }
    47.         }
    48.         if(direction > 0.25f && direction < 0.5f)
    49.         {
    50.             while(timeLeft > 0)
    51.             {
    52.                 transform.Rotate(0, 90, 0);
    53.                 moveDirection = new Vector3(0.25f, 2.77f, positionz);
    54.                 controller.Move(moveDirection * Time.deltaTime);
    55.                 break;
    56.             }
    57.             if (timeLeft < 0)
    58.             {
    59.                 direction = Random.value;
    60.                 timeLeft = 4.0f;
    61.             }
    62.         }
    63.         if(direction > 0.5f && direction < 0.75f)
    64.         {
    65.             while(timeLeft > 0)
    66.             {
    67.                 transform.Rotate(0, 270, 0);
    68.                 moveDirection = -new Vector3(0.25f, 2.77f, positionz);
    69.                 controller.Move(moveDirection * Time.deltaTime);
    70.                 break;
    71.             }
    72.             if (timeLeft < 0)
    73.             {
    74.                 direction = Random.value;
    75.                 timeLeft = 4.0f;
    76.             }
    77.         }
    78.         if(direction > 0.75f && direction < 1.1f)
    79.         {
    80.             while(timeLeft > 0)
    81.             {
    82.                 transform.Rotate(0, 360, 0);
    83.                 moveDirection = -new Vector3(positionx, 2.77f, 0.25f);
    84.                 controller.Move(moveDirection * Time.deltaTime);
    85.                 break;
    86.             }
    87.             if (timeLeft < 0)
    88.             {
    89.                 direction = Random.value;
    90.                 timeLeft = 4.0f;
    91.             }
    92.         }
    93.     }
    94. }
     
  2. Fynn_GER

    Fynn_GER

    Joined:
    Jun 30, 2020
    Posts:
    9
    I solved the Problem with the Y Position by my own but i still dont know why my gameobject rotates.
     
  3. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Lines 52, 67 and 82 all change the rotation.
     
    Fynn_GER likes this.
  4. Fynn_GER

    Fynn_GER

    Joined:
    Jun 30, 2020
    Posts:
    9
    thanks