Boolean data type
A simple walker script for Unity 3D in C# that tries to avoid obstacles. It has been used in the ProXedural project at art school, and it made the cows walk towards one of the players. To be honest, it does not really deserve the I in AI. But then again, no one expects cows to be very clever.
Maybe someone can use it as a base to produce something better (oh well, who am I kidding, this has already been done).
using UnityEngine; using System.Collections; /// <summary> /// A simple AI walking script. Follows a target. /// </summary> public class AIWalker : MonoBehaviour { internal Vector3 target; private CharacterController controller; private int counter = 0; private Vector3 avoidNormal; private Vector3 semiStaticDirection; private RaycastHit hit; /// <summary> /// The delegate that will execute the current state of the FSM. /// </summary> private delegate void FState (); private FState stateMethod; /// <summary> /// Returns the vector of the current forward motion of the controller. /// </summary> /// <value> /// Returns a vector. /// </value> private Vector3 forward { get { return transform.TransformDirection (Vector3.forward); } } /// <summary> /// Helper function to get a right or left vector. /// </summary> /// <value> /// Returns left of right (Vector3). /// </value> private Vector3 randomLeftOrRight { get { return UnityEngine.Random.value > 0.5 ? Vector3.right : Vector3.left; } } /// <summary> /// Calculates the direction to the target object.
How not to do it:
using System; using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// Needs work. /// </summary> public class Inventory : MonoBehaviour { private Dictionary<string, int> backpack; private bool showInventory; void Awake () { } void Start () { DisplayInventory (true); } public void DisplayInventory (bool state) { showInventory = state; } public bool RemoveObject (string iObject, int number) { try { backpack[iObject] = backpack[iObject] - number; if (backpack[iObject] <= 0) { backpack.Remove (iObject); } return true; } catch (Exception ex) { print ("[" + this.GetType ().ToString () + "] The object " + iObject + " does not exist."); return false; } } public bool AddObject (string iObject, int number) { if (backpack.ContainsKey (iObject)) { backpack[iObject] += number; } else { backpack[iObject] = number; } return true; } void OnGUI () { if (showInventory) { int counter = 0; foreach (KeyValuePair<string, int> kv in backpack) { string displayString = kv.Key + ": " + kv.Value.ToString (); GUI.Label (new Rect (10, counter * 40, 200, 30), displayString); counter++; } } } }
How to do it instead:
using System; using UnityEngine; using System.Collections; public class ChickenCounter : MonoBehaviour { private int counter = 0; public GameObject particleEffect; void OnTriggerEnter (Collider hit) { if (hit.tag == "bonus") { counter++; Instantiate (particleEffect, hit.transform.position, hit.transform.rotation); Destroy (hit.gameObject); } } void OnGUI () { GUI.Label(new Rect(10,10,200,30),
