News Forums RAIN General Discussion and Troubleshooting Make NPC wander random in area

This topic contains 10 replies, has 3 voices, and was last updated by  marcfielding 2 years, 6 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #32996

    3dGhost
    Participant

    Hi.
    I start using Rain yesterday, i watched the tutorials but I can’t make my NPC wander how it should. I have a market place and a few NPC, what I want is NPCs move around in the market area. I use the script from CodersExpo (thanks for helping us) found here: .Everything looks good in the first seconds but after that all my NPC form a flock and move to next target location.How can i make them move more natural ?
    I appreciate any help, thanks a lot.

    Here are some images:
    Here looks good...

    BT

    • This topic was modified 2 years, 6 months ago by  3dGhost.
    • This topic was modified 2 years, 6 months ago by  3dGhost.
    • This topic was modified 2 years, 6 months ago by  3dGhost.
    #33008

    prime
    Keymaster

    A couple of things:

    1) You don’t necessarily need a WaypointPath. It’s fine if you intend for your AI to stick to the paths, but just want to make sure you understand how it works.

    2) Can you post your custom action script?

    #33011

    3dGhost
    Participant

    Hi.
    Thank you for taking your time to look at my problem.
    1) The WaypointPath it is from another test, this one is ok.

    2) Here is the script :

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Core;
    using RAIN.Action;
    using RAIN.Navigation;
    using RAIN.Navigation.Graph;
    [RAINAction]
    public class ChooseRandom : RAINAction
    {
    	private RAIN.Navigation.Targets.NavigationTarget _target;
    	public ChooseRandom()
    	{
    		actionName = "ChooseRandom";
    	}
    	public override void Start(AI ai)
    	{
    		_target = NavigationManager.Instance.GetNavigationTarget("PointPosition");
    		base.Start(ai);
    	}
    	public override ActionResult Execute(AI ai)
    	{
    		Vector3 loc = Vector3.zero;
    		List<RAINNavigationGraph> found = new List<RAINNavigationGraph>();
    		do
    		{
    			loc = new Vector3(ai.Kinematic.Position.x + Random.Range(-10f, 10f),
    			                  ai.Kinematic.Position.y + 0f,
    			                  ai.Kinematic.Position.z + Random.Range(-10f, 10f));
    			setFlushToSurface(loc);
    			found = NavigationManager.Instance.GraphsForPoints(
    				ai.Kinematic.Position, 
    				_target.Position, 
    				ai.Motor.StepUpHeight, 
    				NavigationManager.GraphType.Navmesh, 
    				((BasicNavigator)ai.Navigator).GraphTags);
    		} while ((Vector3.Distance(ai.Kinematic.Position, _target.Position) < 2f) || (found.Count == 0));
    		ai.WorkingMemory.SetItem<Vector3>("wanderTarget", _target.Position);
    		return ActionResult.SUCCESS;
    	}
    	public override void Stop(AI ai)
    	{
    		base.Stop(ai);
    	}
    	private void setFlushToSurface(Vector3 targetPosition)
    	{
    		var v = RAIN.Utility.MathUtils.CastToCollider(targetPosition, Vector3.down, 0f, 1000f);
    		var vv = v - _target.Position;
    		if(vv.magnitude > 0f)
    		{
    			_target.Position = v;
    		}
    	}
    }
    

    PS. I guess it’s something simple, but for a noob like me…

    #33012

    3dGhost
    Participant

    Looks like your post have already helped me.
    After a break I just check this script and looks like it’s working.Weird, because I have test it many times with no luck.

    
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Core;
    using RAIN.Action;
    using RAIN.Navigation;
    using RAIN.Navigation.Graph;
    [RAINAction]
    public class WanderLocation : RAINAction
    {
    	public WanderLocation ()
    	{
    		actionName = "WanderLocation";
    	}
        public override void Start(AI ai)
        {
            base.Start(ai);
        }
        public override ActionResult Execute(AI ai)
        {
    		var loc = Vector3.zero; //Default
    		//Create a navigation graph collection.
    		List<RAINNavigationGraph> found = new List<RAINNavigationGraph> ();
    		//Create a vector location based on our AI current location
    		//plus random range values for x and z coordinates
    		do {
    						loc = new Vector3 (ai.Kinematic.Position.x + Random.Range (-15f, 15f),
    			                  ai.Kinematic.Position.y,
    			                  ai.Kinematic.Position.z + Random.Range (-15f, 15f));
    						//We will create navigation points using the above calculated value, the AI current position and ensure that it is within the bounds of our navigation graph
    						found = NavigationManager.Instance.GraphsForPoints(ai.Kinematic.Position
    			                                            					, loc
    			                                             					, ai.Motor.StepUpHeight
    			                                            					, NavigationManager.GraphType.Navmesh
    			                                                				, ((BasicNavigator)ai.Navigator).GraphTags);
    				} while ((Vector3.Distance(ai.Kinematic.Position, loc) <2f) || (found.Count == 0));
    		//We will define a runtime variable in the AIRigs Memory element panel.
    		ai.WorkingMemory.SetItem<Vector3>("varMoveTo", loc);
    		return ActionResult.SUCCESS;
        }
        public override void Stop(AI ai)
        {
            base.Stop(ai);
        }
    }
    

    Now seems I have another 2 problems:
    1) Some of my NPCs get stuck on some Waypoints and start turning around.
    2) They hit in each other sometimes.
    I use the same BehaviorTree from the picture in the first post.
    PS:Sorry for my english.

    • This reply was modified 2 years, 6 months ago by  3dGhost.
    #33018

    prime
    Keymaster

    A couple more thoughts:

    - You can get rid of the Start and Stop overrides
    - Do the NPCs get stuck on only the final Waypoint in the path (right before heading to their target) or just randomly?

    Regarding the AI colliding with each other - the current version of RAIN doesn’t support obstacle avoidance. That’s a feature we’re trying to schedule right now (just got out of a meeting where we discussed it). In the meantime, you can add your own obstacle avoidance by making a mod to the Motor. Contact us directly (support at rival theory dot com) if you have questions regarding how to do that.

    #33028

    3dGhost
    Participant

    I will get rid off Start and Stop.

    They get stuck randomly. So they start turning around a few times and then go back to wander like nothing happens.For the target i use the script from earlier post and the same BehaviorTree.

    Thank you for the great RAIN. I started use it just a few days ago but I love it.

    #33044

    prime
    Keymaster

    I haven’t had time to test it just yet. My first guess is that the wander location chooser allows the “go to” point to be very close to the current AI position. (It can be as close as 2 units away). This means if you get unlucky (which may happen more often than you think) the AI could appear to be just turning around a few times. I don’t know if that’s what is happening in your case, but it seems possible.

    #33049

    3dGhost
    Participant

    Ok, then I will take a look at the script and see if the distance is the problem.
    Thanks.

    #33058

    prime
    Keymaster

    We have found a couple of other issues with facing code that could cause a problem in some cases. Doesn’t sound like your issue, but make sure you grab the 2.1.2 release today and re-test, just in case.

    #33062

    3dGhost
    Participant

    Ok, i will update and see if that help. I think some kind of “wander area” would be perfect in future updates.

    #33228

    marcfielding
    Participant

    Just to chip in here obstacle(or entity) avoidance would be awesome, currently i’m using RAIN for my turrets and its absolutely perfect, I’m just about to add some AI characters though and having them avoid each other would be rather cool, i’m aware we could probably do proximity detection for entities manually via a detect/action node but having it built in would be purrfffect!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.