News Forums RAIN General Discussion and Troubleshooting click to move PC using RAIN

This topic contains 11 replies, has 2 voices, and was last updated by  Makarios 3 months, 1 week ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #34150

    Makarios
    Participant

    Ok, I am trying to make a click to move game using RAIN navmesh / pathfinding to make things simple across the game

    in theory this seems pretty simple to me, navmesh already created for npcs so nothing new there, AI rig the PC’s and make a new behavior tree basically telling them to move & animate to a specific navigation point

    then a quick C# script similar to any other click to move, raycast screen point to ray, find point of hit on terrain,

    *** Here is where my difficulty comes
    I have a specific point from my screenpointtoray, that’s where i want to create my RAIN navigation point

    how do I achieve this?

    I’ve searched through the forums and found lots of conflicting / confusing answers due to version changes and different methods, and the wiki doesn’t have enough description of the RAIN AI classes and functions for me to know what I need from what I’ve been able to see

    I know there are classes for
    RAIN.Core
    RAIN.Navigation
    Rain.Action
    and several others, but i cant find a good description of what they are / what i can use them for / which I need

    and once i have the correct classes pulled for the script what is the actual command I would use to create my Navigation point named “pcmovepoint” at that point named “clickpoint”, and delete any other points that had been previously created named “pcmovepoint”?

    Thanks ahead of time for any help with this

    #34153

    prime
    Keymaster

    The answer is pretty simple and easy to implement.

    Once you have your move-to position from your raycast, simply set a variable in AI memory with that position. e.g.,

    Vector3 moveToPosition; //Set this to the position you want to move to
    ai.WorkingMemory.SetItem<Vector3>("moveTarget", moveToPosition); //moveTarget is the AI memory variable I'm using

    Then in your behavior tree just set the Move Target of your move node to the variable you used (in my case, moveTarget).

    #34158

    Makarios
    Participant

    one followup, in order to call the ai.workingmemory.setitem do i need to bring in any of the RAIN classes and if so which?

    RAIN.Core?
    RAIN.Navigation?
    RAIN.Action?

    #34160

    prime
    Keymaster

    Just RAIN.Core

    #34163

    Makarios
    Participant

    getting a compile error that the name ai does not exist in the current context, cant get any of the autofill to pull up ai.workingmemory.setitem….

    using UnityEngine;
    using System.Collections;
    using RAIN.Core;

    public class clickmove : MonoBehaviour
    {
    void Update ()
    {
    if (Input.GetMouseButtonDown(0))
    {
    Ray _mouseclick = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit _clickpoint;
    if (Physics.Raycast(_mouseclick, out _clickpoint))
    {
    ai.WorkingMemory.SetItem<Vector3>(“pcmovepoint”, _clickpoint);
    }
    }
    }
    }

    #34164

    Makarios
    Participant

    is the problem because it’s pulling MonoBehaviour? what else should i call?

    #34165

    prime
    Keymaster

    In your code, you need to find the AIRig on your AI through a GetComponent() call. If it is on the same game object as your clicktomove behaviour, then it will be

    AIRig tRig = gameObject.GetComponent<AIRig>();
    ...
    tRig.AI.WorkingMemory.SetItem<Vector3>(“pcmovepoint”, _clickpoint);
    #34169

    Makarios
    Participant

    I think we’re getting close, not sure if it’s just a syntax problem now or what

    here’s my code now:

    using UnityEngine;
    using System.Collections;
    using RAIN.Core;

    public class clickmove : MonoBehaviour
    {
    public AIRig _PCRig;

    void Start ()
    {
    AIRig _PCRig = gameObject.GetComponent<AIRig>();
    }

    void Update ()
    {
    if (Input.GetMouseButtonDown(0))
    {
    Ray _mouseclick = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit _clickpoint;

    if (Physics.Raycast(_mouseclick, out _clickpoint))
    {
    _PCRig.AI.WorkingMemory.SetItem<Vector3>(“pcmovepoint”, _clickpoint.point);
    }
    }
    }
    }

    for line 11 getting an error that variable _PCRig is assigned but never used
    for line 23, every time i click it causes a “Object reference not set to an instnace of an object” error

    #34170

    prime
    Keymaster

    line 11 should be

    _PCRig = gameObject.GetComponent<AIRig>();
    #34171

    Makarios
    Participant

    k, quick fix for 11, i still get the line 23 error every time I click tho

    _PCRig.AI.WorkingMemory.SetItem<Vector3>(“pcmovepoint”, _clickpoint.point);

    object reference not set to an instance of an object

    #34174

    prime
    Keymaster

    Change GetComponent to GetComponentInChildren, see if that helps

    #34180

    Makarios
    Participant

    i made that line public to make sure that was not the problem, dragged the airig directly into the public var, still gets the same error.

    **edit

    I stand corrected, i didnt comment out my previous line and it was overriding, but after fixing yes i stop getting the error and behavior seems to be picking up the variable now

    gonna clean it up some tomorrow but I’ll post the final basic code for click to move here afterwards

    Thanks again for the help

    • This reply was modified 3 months, 1 week ago by  Makarios.
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.