How to use the Unity nav mesh in Rain« Back to Previous Page


Is it possible and relatively easy to use the unity baked nav mesh in Rain?

I have a standard terrain 2,000 by 2,000 by 600 high
I've created mountains valleys etc.
I'm creating a nav mesh so the creatures can wander around and chase each other.

If I try to make it using the rain nav mesh it just doesn't work I think its just too big

But I can make one quite quickly (5 seconds) using the unity Bake navigation mesh.

So I was thinking that I might use that nav mesh with rain move action
Posted by nomax5 (Questions:3, Answers:4)
Asked on November 10, 2022 1:06 pm
0

Just create own Motor

using RAIN.Core;
using RAIN.Serialization;
using UnityEngine;

namespace RAIN.Motion
{
[RAINSerializableClass]
public class NavMeshMotor : RAINMotor
{
NavMeshAgent _agent;
Vector3 _currentTarget;

[RAINSerializableField(ToolTip = "Default stopping distance", Visibility = FieldVisibility.Show)]
public float DefaultCloseEnoughDistance = 0.1f;

public override void AIInit()
{
base.AIInit();
_agent = _ai.Body.GetComponent();
CloseEnoughDistance = DefaultCloseEnoughDistance;
}

public override void ReInitialize()
{
}

public override void UpdateMotionTransforms()
{
_ai.Kinematic.Position = this._ai.Body.transform.position;
}

public override void ApplyMotionTransforms()
{

}

public override bool MoveTo(Vector3 position)
{
moveTarget.VectorTarget = position;
//Target = position;
return this.Move();
}

public override bool Move()
{

if (_agent == null || moveTarget.VectorTarget == Vector3.zero)
{
return true;
}

if (_currentTarget != moveTarget.VectorTarget)
{
//Debug.Log(”Set destination”);
_currentTarget = moveTarget.VectorTarget;
_agent.SetDestination(moveTarget.VectorTarget);
}

if (!_agent.pathPending)
{
//Debug.Log(_agent.remainingDistance);
if (_agent.remainingDistance <= CloseEnoughDistance)
{
//Debug.Log("sucess");
return true;
}
}
return false;
}

public override bool FaceAt(Vector3 position)
{
return true;
}

public override bool Face()
{
return true;
}

public override void Stop()
{
_agent.Stop();
}
}
}

Posted by itmindco (Questions:0, Answers:1)
Answered On November 27, 2022 1:52 am #
0
@nomax5,
I am using the Unity NavMesh and NavMeshAgents with my RAIN NPCs. I am sure I'm doing something wrong, but for my characters, I couldn't get my NPCs to avoid each other and I needed to have my characters walk on certain areas only. I ended up switching over to Unity's NavMesh and set up planes along the path that I wanted and assigned those to a custom NavMesh layer. Then, in the Inspector, I set the NavMeshAgent to use my custom layer only.

Regarding your code above (or below after I post this), one thing I noticed, and I am guessing it is just a typo. In your start function:

nma = ai.Body.GetComponent();

Should be:

nma = ai.Body.GetComponent<NavMeshAgent>();

Aside from that, make sure that you attach the NavMeshAgent, tweak whatever settings you want and then make sure that the NavMesh Walkable entry (bottom of the NavMeshAgent component in the Inspector) set to Everything and your character should be able to move around.

Another reason I ended up using the Unity NavMeshAgent was that for some reason I have yet to uncover, the characters when controlled by RAIN don't seem to play the full animation. Won't bore you with the details here.

Hope that helps some.
Posted by Aaron Mueller (Questions:0, Answers:4)
Answered On November 12, 2022 3:13 pm #
0

If you want to use Unity’s nav mesh system, you have to use Unity’s NavMeshAgent for movement. To do that, you will have to create a custom RAINMotor that does not try to manage the position of the AI.

I’ll see if I can post an example fairly soon.

Posted by prime (Questions:2, Answers:127)
Answered On November 12, 2022 3:13 pm #
0

I've adapted the code slightly to look like the newer custom action code (removed the float deltaTime bit etc.

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

[RAINAction]
public class NavMove : RAINAction
{
NavMeshAgent nma;
Vector3 dest = new Vector3(64f, 0f, 50f);

public NavMove()
{
actionName = "NavMove";
}

public override void Start(AI ai)
{
nma = ai.Body.GetComponent();
nma.SetDestination(dest);
}

public override ActionResult Execute(AI ai)
{
if ((ai.Body.transform.position - dest).magnitude < 1f)
return ActionResult.SUCCESS;

return ActionResult.RUNNING;
}

public override void Stop(AI ai)
{
base.Stop(ai);
}
}

Should this be working?
I created a unity navmesh
created a navmesh agent (rsized it etc.)
dragged it on to EuropeanBison / AI in the Hierachy so now its below the Black rain AIRig (script) in the inspector

I created a custom action called navMove (little n) give it a class of NavMove (big N)

What do I do now lol?


Sorry to be asking such thickie questions but I can get my bison to chase me all over the map with a little script that just
GetComponent<NavMeshAgent>().destination = Player.position;

But I really want to use rain for the AI

Any help grealy appreciated.
Posted by nomax5 (Questions:3, Answers:4)
Answered On November 10, 2022 9:19 pm #
0

I've been digging around in old notes and this question came up in the old forums and Jester answered it with a bit of code. I'll paste his answer below:


Thanks for the post. If you'd like to use Unity's navmeshes but want to continue to use RAIN for behavior, one approach would be to build a custom behavior tree action for handling movement.

Create and AI with a BehaviorTreeMind and set up a behavior tree. Then, attach Unity's NavMeshAgent component to the AI. Finally, create a custom action that uses the NavMeshAgent to move the AI. The code below shows how this would be done:

public class NavMove : Action
{
NavMeshAgent nma;
Vector3 dest = new Vector3(64f, 0f, 50f);

public NavMove()
{
actionName = "NavMove";
}

public override void Start(AI ai, float deltaTime)
{
nma = ai.Body.GetComponent();
nma.SetDestination(dest);
}

public override ActionResult Execute(AI ai, float deltaTime)
{
if ((ai.Body.transform.position - dest).magnitude < 1f)
return ActionResult.SUCCESS;

return ActionResult.RUNNING;
}

public override void Stop(AI ai, float deltaTime)
{
}
}
Posted by nomax5 (Questions:3, Answers:4)
Answered On November 10, 2022 9:03 pm #