News Forums RAIN General Discussion and Troubleshooting AI Interaction with other GameObjects

This topic contains 1 reply, has 1 voice, and was last updated by  Malkavan 4 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #40307

    Malkavan
    Participant

    Hey All,

    I would much appreciate if anyone can point me in the right direction. I currently have a moba style setup with units following a path, engaging with each other, playing attack animations etc. All is looking good. Only I have tried a few methods and look for tutorials on how to for example. On attack animation decrease the health of the target that is being attacked. I have recently bought the advanced warfighter ai project to have a look through and see if I can find the answer but I’m finding it quite hard to understand or get my piece out of all that is in there.

    Basically I’m looking for interaction between AI and Entity. setting variables etc. Anyone have any place they can point me which might help?

    #40308

    Malkavan
    Participant

    An update! I’ve gone and done it!

    Thankfully things worked out, a shoutout to Sigil and his post linked here : http://rivaltheory.com/forums/topic/accessing-variables-on-scene-objects-other-than-ai-objects/

    One problem I’ve ran into now. My charachter is chopping a tree. The Tree’s health reaches 0 and is destroyed. I expect that the AI would go to another tree within sensor range, however its just standing there and im getting a consistent null reference exception happening. Any thoughts anyone? Here is my code:

    The Controller (Badly Named)

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    [RAINAction]
    public class CheckHealth : RAINAction
    {
        private ResourceManager Wood = null;
        private TreeHealth _targetHealth = null;
        public override void Start(RAIN.Core.AI ai)
        {
            base.Start(ai);
            GameObject ResourceManagerGO = GameObject.Find("ResourceManager");
            Wood = ResourceManagerGO.GetComponent<ResourceManager>();
            _targetHealth = null;
            // This was detected and assigned to the Form Variable "target"
            GameObject tTargetObject = ai.WorkingMemory.GetItem<GameObject>("TreeSeen");
            if (tTargetObject != null)
                _targetHealth = tTargetObject.GetComponent<TreeHealth>();
        }
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            // Look at the component and return failure if it has no health
            if (_targetHealth == null || _targetHealth.Health <= 0)
            {
                _targetHealth.exhausted = true;
                return ActionResult.FAILURE;
            }
            _targetHealth.Health -= 1;
            Wood.Wood += 1;
            Debug.Log("This is actually working, Target Health: " + _targetHealth.Health + " Wood Count: "+ Wood.Wood);
            return ActionResult.SUCCESS;
        }
        public override void Stop(RAIN.Core.AI ai)
        {
            base.Stop(ai);
        }
    }

    The script killing the Tree:

    using UnityEngine;
    using System.Collections;
    public class IfDead : MonoBehaviour {
        TreeHealth thisTree = null;
    	// Use this for initialization
    	void Start () {
            thisTree = this.gameObject.GetComponent<TreeHealth>();
    	}
    	// Update is called once per frame
    	void Update () {
            if (thisTree.exhausted == true)
                Destroy(this.gameObject);
    	}
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.