I was making a game and I wanted to create an AI bot which could simulate key presses and when it fails it retries and it notes on how it failed last time and corrects its self. I sat up last night writing this code but I doesn't work at all.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Script : MonoBehaviour {
int[] FirstTry = new int[3];
int[] SecondTry = new int[3];
int[] ThirdTry = new int[3];
int[] FourthTry = new int[3];
int[] FifthTry = new int[3];
int[] SixthTry = new int[3];
int FRT = 0;
int FT = 0;
int ST = 0;
int TT = 0;
int FFT = 0;
int SXT = 0;
public int W = 1;
public int A = 2;
public int S = 3;
public int D = 4;
int test;
int numbTests = 6;
float ev2Sec = 2;
float regenTime = 2;
IList hi;
public Vector3 startTrans;
public bool isRunning = true;
// Use this for initialization
void Start () {
startTrans = transform.position;
}
// Update is called once per frame
void Update () {
ev2Sec -= Time.deltaTime;
if (ev2Sec < 0) {
if (numbTests > 0) {
test = Random.Range(0, 4);
if (numbTests == 3) {
foreach (int x in FirstTry)
{
if (x.Equals (test))
{
test = Random.Range(0, 4);
}
}
}
else if (numbTests == 2) {
foreach (int x in SecondTry)
{
if (x.Equals (test))
{
test = Random.Range(0, 4);
}
}
}
else if (numbTests == 4) {
foreach (int x in FifthTry)
{
if (x.Equals (test))
{
test = Random.Range(0, 4);
}
}
}
else if (numbTests == 5) {
foreach (int x in SixthTry)
{
if (x.Equals (test))
{
test = Random.Range(0, 4);
}
}
}
else if (numbTests == 1) {
foreach (int x in ThirdTry)
{
if (x.Equals (test))
{
test = Random.Range(0, 4);
}
}
}
else if (numbTests == 0) {
foreach (int x in FourthTry)
{
if (x.Equals (test))
{
test = Random.Range(0, 4);
}
}
}
if (test == 1) {
transform.position = Vector3.MoveTowards(transform.position, transform.forward * 10, 10);
ev2Sec = regenTime;
numbTests -= 1;
}
if (test == 2) {
transform.position = Vector3.MoveTowards(transform.position, transform.right * -10, 10);
ev2Sec = regenTime;
numbTests -= 1;
}
if (test == 3) {
transform.position = Vector3.MoveTowards(transform.position, transform.forward * -10, 10);
numbTests -= 1;
ev2Sec = regenTime;
}
if (test == 4) {
transform.position = Vector3.MoveTowards(transform.position, transform.right * 10, 10);
ev2Sec = regenTime;
numbTests -= 1;
}
}
else {
numbTests = 6;
}
}
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Ground") {
isRunning = false;
print("NumbTest" + numbTests);
print ("GETTINGCALLED");
transform.position = startTrans;
print ("First" + FT);
print ("Second" + ST);
print ("Third" + TT);
print ("Fourth" + FRT);
print ("Fifth" + FFT);
print ("Sixth" + SXT);
if (numbTests == 0)
if (FRT == 0) {
isRunning = true;
FourthTry[0] = test;
FRT += 1;
numbTests = 6;
}
else {
FRT += 1;
print (FRT);
isRunning = true;
FourthTry[FRT] = test;
print (FourthTry);
numbTests = 6;
}
if (numbTests == 1)
if (TT == 0) {
isRunning = true;
ThirdTry[0] = test;
TT += 1;
numbTests = 6;
}
else {
TT += 1;
isRunning = true;
ThirdTry[TT] = test;
numbTests = 6;
}
if (numbTests == 2)
if (ST == 0) {
isRunning = true;
SecondTry[0] = test;
ST += 1;
numbTests = 6;
}
else {
ST += 1;
isRunning = true;
SecondTry[ST] = test;
numbTests = 6;
}
if (numbTests == 3)
if (FT == 0) {
isRunning = true;
FirstTry[0] = test;
FT += 1;
numbTests = 6;
}
else {
FT += 1;
isRunning = true;
FirstTry[FT] = test;
numbTests = 6;
}
if (numbTests == 4)
if (FFT == 0) {
print ("SOMETHING");
isRunning = true;
FifthTry[0] = test;
FFT += 1;
numbTests = 6;
}
else {
print ("SOMETHINGZZZZ");
FFT += 1;
isRunning = true;
FifthTry[FFT] = test;
numbTests = 6;
}
if (numbTests == 5)
if (SXT == 0) {
isRunning = true;
SixthTry[0] = test;
SXT += 1;
numbTests = 6;
}
else {
SXT += 1;
isRunning = true;
SixthTry[SXT] = test;
numbTests = 6;
}
}
}
}
I thought that this would work but I just gave me random things I didn't expect like the same key being repeated over and over again.
What I was trying to achieve with this script was, I have 6 moves and each move it chooses out of WASD and if the player falls out of the block of cube and falls to the cube with the tag of ground then the last key that was pressed will be added to an array. This array is decided on which move you are on. So if the bot pressed A on Move .no 3. The number 2 will be added to ThirdTry array. Then when the random number is generated if it checks if the random number exists to the array and if it does it creates a new random number...
Yeah I know. I tried very very hard and this is as far as I go with AI and bots taking into consideration Im only 12 years old. :P
Please can anyone help me by improving this code I already have or a sample code of how they would execute this process or even a tutorial on this subject. Thank you very much, also please tell me what I did wrong in this code if you notice anything.
Thank you very much, please help a small boy with big dreamz. :D
Aucun commentaire:
Enregistrer un commentaire