Search Unity

AI Decisions Randomization Problem

Discussion in 'Scripting' started by prasanth-akon, Sep 25, 2015.

  1. prasanth-akon

    prasanth-akon

    Joined:
    May 30, 2013
    Posts:
    16
    Hello,
    I have two classes. One for AI and other is actually Node based Waypoints.
    There are Two Nodes
    1)Normal Node-Continue on default waypoints
    2)Intersection Node-Chooses one randomly predefined intersection node and continues from it
    So when AI reach a Normal Node, it continues on its waypoint.
    Code (CSharp):
    1.  
    2. if (Node.ChildNode.CurrentNode == Node.TotalNodes.Normal) {
    3. Agent.SetDestination (path [currentPathObj].position);
    4. }
    5.  
    If the AI reaches an Intersection Node, It has two options based on Random.Range.
    If the value is 0
    Code (CSharp):
    1. int random = Mathf.RoundToInt (Random.Range (0, 1));
    2. if (random == 0)
    3. {
    4.     currentPathObj++;
    5.     Agent.SetDestination (path [currentPathObj].position);
    6. }
    If the value is 1
    Code (CSharp):
    1.  
    2. else if (Node.ChildNode.CurrentNode == Node.TotalNodes.Intersection) { {
    3.                
    4.  
    5.                     int NodeSelect = Mathf.RoundToInt (Random.Range (0, Node.ChildNode.TotalAdjacentNodes));
    6.                     switch (NodeSelect) {
    7.                
    8.                     case 0:
    9.                         Agent.SetDestination (Node.ChildNode.AdjacentNodes [NodeSelect].transform.position);
    10.                         break;
    11.                     case 1:
    12.                         Agent.SetDestination (Node.ChildNode.AdjacentNodes [NodeSelect].transform.position);
    13.                         break;
    14.                     case 3:
    15.                         Agent.SetDestination (Node.ChildNode.AdjacentNodes [NodeSelect].transform.position);
    16.                         break;
    17.                     case 4:
    18.                         Agent.SetDestination (Node.ChildNode.AdjacentNodes [NodeSelect].transform.position);
    19.                         break;
    20.  
    21.                     }
    22.                 }
    This is the node class
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class Node : MonoBehaviour
    5. {
    6.     public enum TotalNodes
    7.     {
    8.         Normal,
    9.         Intersection
    10.     }
    11.     ;
    12.  
    13.     public TotalNodes CurrentNode;
    14.     public List<GameObject> AdjacentNodes = new List<GameObject> ();
    15.     public static Node ChildNode;
    16.     public int TotalAdjacentNodes;
    17.     // Use this for initialization
    18.     void Start ()
    19.     {
    20.         ChildNode = this;
    21.         TotalAdjacentNodes = AdjacentNodes.Count;
    22.     }
    23.  
    24.     void OnDrawGizmos ()
    25.     {
    26.         if (CurrentNode == TotalNodes.Normal) {
    27.             Gizmos.color = Color.blue;
    28.             Gizmos.DrawWireSphere (transform.position, 0.5f);
    29.         } else {
    30.             Gizmos.color = Color.red;
    31.             Gizmos.DrawWireSphere (transform.position, 0.5f);
    32.         }
    33.     }
    34. }
    So my Problem that it always returns Normal node and i cannot see the intersection node happening. So what am i doing wrong?
    Thanks in Advance
     
    Last edited: Sep 25, 2015
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Random Range is exclusive on the maximum value I believe.

    Also you can use ints without casting.

    try this

    int random = Random.Range(0, 2);
     
    martinmr and Kiwasi like this.
  3. prasanth-akon

    prasanth-akon

    Joined:
    May 30, 2013
    Posts:
    16
    I believe it is something to do with the Enum? Because i have specified the Nodes whether it is Normal or intersection in the inspector. But it is only returning only normal. So it is taking only the first value of the total nodes which is normal. I guess is there any way to do it via code?
     
  4. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Also ChildNode is static - is this causing you trouble. You can have many nodes in the scene but ChildNode will be the same for all of them....
     
  5. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Why is ChildNode static ? you're using it to get the enum value so you're always going to get whatever node had its Start() callback last...
     
  6. prasanth-akon

    prasanth-akon

    Joined:
    May 30, 2013
    Posts:
    16
    Oh,So is there anyway to access the Specific Nodes in Node Class from AI class?
    For example:
    If i have 5 node objects:3-normal,2-intersection. How can i get this info from node class to the AI class?
     
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    This will give you everytime 0

    public static function Range(min: int, max: int): int;

    Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).

    so if you want a rnd 0 or 1 you have to make

    int random =Random.Range(0, 2);

    also you don't need any math. round because its an int Random after all.


    on the other hand a float rnd

    public static function Range(min: float, max: float): float;

    Returns a random float number between and min [inclusive] and max [inclusive] (Read Only).

    float random =Random.Range(0f, 1f);

    will give you a number between 0,00000 and 1,0000 so for example 0.13 or 0.56 and so on.
     
  8. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    @prasanth.akon how do you detect your AI reached the node ? Is it trigger based ? Distance based ?

    Either way, you need to gather the reference to the node and use that.

    If you're trigger base you'll do something this in your OnTriggerEnter():
    Code (CSharp):
    1. currentNode = other.GetComponent<Node>();
    If you're distance based or whatever just keep the value somewhere when targeting the node.