Announcement

Collapse
No announcement yet.

Keeping AI Bot movement constrained to a path

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • replied
    I do it in my tower defence game using a spline.

    I set my waypoints using a blueprint that has an array for my custom waypoints on there. I then get the location of them and add them into a spline array and generate the spline.

    Leave a comment:


  • replied
    I created a waypoint system which uses the nav mesh to go from one waypoint to the next waypoint. Once a character reaches a waypoint, he performs any specific action at that waypoint position and then resumes onto the next waypoint. This lets me create arbitrary paths which any actor can follow.

    I wrote my system in C++ and I'm willing to share my header file code:

    Code:
    UENUM(BlueprintType)
    enum class EWaypointMethod : uint8
    {
    	//We are moving directly to the next waypoint.
    	Direct,
    
    	//We are wandering towards the waypoint
    	Wander,
    
    	//we are moving to the next waypoint but we may interrupt our travels to do something more important, such as attacking an enemy.
    	Patrol
    };
    
    
    /*Creatures may use different actions when*/
    UENUM(BlueprintType)
    enum class EActionType : uint8
    {
    	//the creature is not doing anything
    	None,
    
    	//The creature is going through a spawning sequence
    	Spawning,
    
    	//the creature is doing a melee attack
    	Attack_Melee,
    
    	//the creature is doing a grab attack
    	Attack_Grab,
    
    	//the creature is doing a smash attack
    	Attack_Smash,
    
    	//creature is performing a ranged attack
    	Attack_Ranged,
    
    	//creature is doing a special attack type 1
    	Attack_Special1,
    
    	//creature is doing a special attack type 2
    	Attack_Special2,
    
    	//creature is doing a special attack type 3
    	Attack_Special3,
    
    	//creature is taking damage
    	TakeDamage,
    
    	//creature is performing its celebration activity, whatever that is.
    	Celebrate,
    
    	//the creature is searching around the area
    	Search,
    
    	//the creature is teleporting out
    	Phase_Out,
    
    	//the creature is teleporting in
    	Phase_In
    };
    
    
    UCLASS()
    class AWaypoint : public AActor
    {
    	GENERATED_BODY()
    
    public:
    	//The world position of the waypoint
    	//UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Waypoint", meta = (MakeEditWidget))
    	//FVector Position;
    
    	//how close a character has to get to the waypoint for it to be considered "reached"
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Waypoint")
    	float AcceptanceRadius = 50;
    
    	//might want to add an arrow component so that we can see the waypoint in the editor
    	UPROPERTY()
    	USceneComponent* Root;
    
    	UPROPERTY()
    	UArrowComponent* Arrow;
    
    	UPROPERTY()
    	USphereComponent* Area;
    
    	UPROPERTY()
    	UTextRenderComponent* Label;
    
    	/*The character moving to this waypoint will run at full speed if this is checked.*/
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Waypoint")
    	bool bShouldRun = false;
    
    	/*If you want your character to perform a particular action when they arrive at this waypoint, you can set that here.*/
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Waypoint")
    	EActionType OnArriveAction = EActionType::None;
    
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Waypoint")
    	bool RefreshPath = false;
    
    	/*After reaching this waypoint, a character will go to the next waypoint.*/
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Waypoint")
    	AWaypoint* NextWaypoint;
    
    	AWaypoint(const FObjectInitializer& ObjectInitializer);
    
    	UFUNCTION(BlueprintCallable, Category = "Waypoint")
    		float GetAcceptanceRadius() const;
    
    	UFUNCTION()
    		virtual void OnConstruction(const FTransform& inTransform) override;
    
    	/*This gets the location of the waypoint*/
    	UFUNCTION(BlueprintCallable, Category = "Waypoint")
    		FVector GetLocation() const;
    
    	/*Note: If the player is being guided to a waypoint, we may want to draw some sort of in-game path by moving some sort of visual effect
    	along the waypoint nav path*/
    
    	/**
    	This gives you the navigation path necessary to get to the current waypoint / objective.
    	@param ForActor This is a reference to the actor which we're generating the path for.
    	@param StepSize This is how many increments we have between our vectors
    	@param NavHeight This is the ray delta height we use to follow contours of navigable terrain. Use smaller values indoors!
    	*/
    	UFUNCTION(BlueprintCallable, Category = "Waypoint")
    	TArray<FVector> GetNavPath(AActor* ForActor, float StepSize = 100, float NavHeight = 500) const;
    };
    
    UCLASS()
    class AWaypointList : public AActor
    {
    	GENERATED_BODY()
    
    public:
    
    	AWaypointList(const FObjectInitializer& ObjectInitializer);
    
    	UPROPERTY(VisibleAnywhere, Category = "Default")
    	USceneComponent* Root;
    
    	UPROPERTY(VisibleAnywhere, Category = "Default")
    		UBillboardComponent* Billboard;
    
    	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Default", meta = (MakeEditWidget))
    		TArray<AWaypoint*> WaypointList;
    
    	UPROPERTY(EditAnywhere, Category = "Default")
    	bool bShowDebugInfo = false;
    
    	UFUNCTION()
    	virtual void OnConstruction(const FTransform& inTransform) override;
    
    	/*Note: I think the waypoints we insert are going to be relatively positioned to this list, so I should verify the positioning.*/
    
    };

    Leave a comment:


  • replied
    While not nearly an expert on this subject, the first thing that comes to my mind is to place an array of target points along your path and use the moveto to go from one to the other.

    Just a thought. Hopefully someone with more experience with this comes along after me and has a better solution for you. Have a great day!

    Leave a comment:


  • started a topic Keeping AI Bot movement constrained to a path

    Keeping AI Bot movement constrained to a path

    Hi - I want a spawned AI Bot to run from point A to Point B. I have this working via a "Simple Move To Location" node and specifying the vector of the final area I want the Bot to run to.
    I've also prevented the Bot running diagonally from A-B by creating a maze like series of Nav Mesh Bounds Volumes.

    While this works I'm wondering if there is a more efficient workflow for a larger environment ?
    The Bot will be running along a curved road and I want the Bot to stay in the center of the road (more or less).
    Is there any way of drawing or importing a spline and constraining the Bots movement along the spline ?
    Click image for larger version

Name:	tpdown.jpg
Views:	1
Size:	183.3 KB
ID:	1170012
Working...
X