playMaker

Author Topic: A* pathfinding with playmaker?  (Read 3134 times)

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
A* pathfinding with playmaker?
« on: January 03, 2013, 07:18:15 PM »
Hi again playmakers :)

I have just fiddled around with the A* free pathfinding and have managed to get an AI moving to a ceartain pre-defined set of coordinates while generating a path and avoiding obstacles on the way

However - What do i do if i want this AI to be an enemy character that chases the player? are there any tools in playmaker where i can make an AI move  while using the pathfinding data i have collected, or do i have to code the chasing alltogether?

This is what i have for the AI so far;

Code: [Select]
using UnityEngine;
using System.Collections;
using Pathfinding;

public class Movescript : MonoBehaviour {
  
    public Vector3 targetPosition;
    private Seeker seeker;
    private CharacterController controller;
 
    public Path path;
    
    public float speed = 100;
    
    public float nextWaypointDistance = 2;
 
    private int currentWaypoint = 0;
 
    public void Start () {
        seeker = GetComponent<Seeker>();
        controller = GetComponent<CharacterController>();
        
        seeker.StartPath (transform.position,targetPosition, OnPathComplete);
    }
    
    public void OnPathComplete (Path p) {
      
            if (!p.error) {
            path = p;
            currentWaypoint = 0;
        }
    }
 
    public void FixedUpdate () {
        if (path == null) {
            return;
        }
        
        if (currentWaypoint >= path.vectorPath.Length) {
            return;
        }
        
        Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
        dir *= speed * Time.fixedDeltaTime;
        controller.SimpleMove (dir);
        
        if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
            currentWaypoint++;
            return;
        }
    }
}

So basically what i understand of this is that it supplies the enemy AI with the target coordinates variable and it generates a path and moves along it to the target

but how would i go about making that variable a global var so i can change it every frame to update coords with the players position, and is that even possible?



in short, I want the enemy AI to chase the player and still avoid collisions, but what i have right now only works with a static coordinate that can't be changed

help please =(

--EDIT:

I now have the player saving his coordinates as a vector3 var and the pathfinder is now taking those coordinates as the travel destination - but again, it only does this once, and if i move it will just walk to the location where i started as a player and then stop there

I have tried changing the start function to an update function without any success

-sorry, im not the strongest in coding (which is why i'm here in the first place)
« Last Edit: January 03, 2013, 07:28:56 PM by KozTheBoss »
Remember, you don't fail unless you give up trying to succeed!

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: A* pathfinding with playmaker?
« Reply #1 on: January 03, 2013, 08:00:03 PM »
define a public variable for your player gameObject instead of one for a vector3

Code: [Select]
public gameObject player;
then in your FixedUpdate, check whether the position you've used last to start a path is the same as the current position of the gameObject (player.transform.position). If it isn't, start a new path with player.transform.position as endpoint. If they are the same, don't do anything (for performance issues).

Have a look at the FollowTarget action in the latest package:
http://hutonggames.com/playmakerforum/index.php?topic=2540.0

It's not 100% perfect but it's close :)

The data you have collected in your script is nothing more than a list of vector3s , so there's no real way to update them unfortunately. On small changes you can set the end point to something different (working on that in the script), but generally you'd recalculate it. It's actually not all that expensive.
Best,
Sven

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: A* pathfinding with playmaker?
« Reply #2 on: January 05, 2013, 08:25:13 AM »
thanks kiriri :) I think i get what you're saying, and ill try my luck :) thanks for the link to the a* actions, ill have a look at them and try to edit to my benefit - will report back on that post if i have any luck :)
Remember, you don't fail unless you give up trying to succeed!