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.
-
AuthorPosts
-
November 14, 2022 at 6:37 am #34150
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 pointhow 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 needand 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
November 14, 2022 at 9:38 am #34153The 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).
November 14, 2022 at 1:18 pm #34158one 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?November 14, 2022 at 2:41 pm #34160Just RAIN.Core
November 15, 2022 at 8:05 am #34163getting 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);
}
}
}
}November 15, 2022 at 4:12 pm #34164is the problem because it’s pulling MonoBehaviour? what else should i call?
November 15, 2022 at 5:21 pm #34165In 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);
November 16, 2022 at 9:23 am #34169I 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” errorNovember 16, 2022 at 10:34 am #34170line 11 should be
_PCRig = gameObject.GetComponent<AIRig>();
November 16, 2022 at 11:07 am #34171k, 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
November 16, 2022 at 12:34 pm #34174Change GetComponent to GetComponentInChildren, see if that helps
November 16, 2022 at 7:54 pm #34180i 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.
-
AuthorPosts
You must be logged in to reply to this topic.