playMaker

Author Topic: Tile Based Map and Navigation  (Read 13972 times)

Lobohotpants

  • Playmaker Newbie
  • *
  • Posts: 38
Tile Based Map and Navigation
« on: July 12, 2012, 04:34:02 PM »
http://forum.unity3d.com/threads/138355-Tile-Based-Map-amp-Nav

Has anyone had experience with this package yet?  I just got it today and have been going through the scripts to see how I well it can integrate with playmaker.  I don't have much experience making custom actions but I'll add anything here if I figure it out.

Lobohotpants

  • Playmaker Newbie
  • *
  • Posts: 38
Re: Tile Based Map and Navigation
« Reply #1 on: July 13, 2012, 06:40:16 PM »
Code: [Select]
void Update()
{
// don;t do anything else if there was not a jouse click
if (!Input.GetMouseButtonUp(0)) return;

// cast a ray to check what the player "clicked" on. Only want to know
// about TILE clicks, so pass mask to check against layer for tiles only
Ray ray = rayCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 500f, tilesLayer))
{
// a tile gameobject was clicked on

// get the TileNode
TileNode node = hit.collider.GetComponent<TileNode>();
if (node == null) return; // sanity check

// dont spawn here if there is alrelady a unit on this tile
if (node.units.Count > 0) return;

// send events to Playmaker
behavior.Fsm.Event("TileNodeActive");

I can send an event here using the built in mouse in put and ray cast but I need to figure out how I can access the individual node ID and position so I can spawn an object on top of the node.

Rlennox

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Tile Based Map and Navigation
« Reply #2 on: July 20, 2012, 04:29:35 AM »
Yeah I just got this too, also trying to see how it could work with playmaker. For now just trying to understand each script and what they do. Custom actions for spawning units, showing radius markers etc would be great.

Rlennox

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Tile Based Map and Navigation
« Reply #3 on: July 20, 2012, 04:57:14 AM »

I can send an event here using the built in mouse in put and ray cast but I need to figure out how I can access the individual node ID and position so I can spawn an object on top of the node.

The unique node ID is a public int idx in the TileNode.cs script attached the node gameobject, but im not sure you would need it in this case. You should be able to use the raycast to get the node gameobject on a mouse click, store it in a GameObject var called something like targetNode, then use that to set the transform position for the unit you are spawning.

Lobohotpants

  • Playmaker Newbie
  • *
  • Posts: 38
Re: Tile Based Map and Navigation
« Reply #4 on: July 20, 2012, 11:56:06 AM »
Rlennox:  haha I just figured that out yesterday.  I did set up something similar, using raycast and mouse pick on mouseover, to create a square marker on the tiles that will follow the mouse cursor. 


I've modified the script above to where I can set the unit that will spawn through the unitFab property and still let the game controller handle where to spawn and that sort of stuff. 

Code: [Select]
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;

public class GameControllerIOW : MonoBehaviour
{
// This is a simple sample of how to spawn a unit on a tile that was clicked

public CameraMove camMover; // used to move camera around (like make it follow a transform)
public Camera rayCam;
public GameObject unitFab; // unit prefab
public MapNav map; // the mapnav
public LayerMask tilesLayer; // layer the tiles are on
public PlayMakerFSM behavior; // playmaker behavior
public int nodeId;
public Vector3 nodeLoc;
public TileNode node = null;





IEnumerator Start()
{
// wait for a frame for everything else to start and then enable the colliders for the TielNodes
yield return null;

// now enable the colliders of the TileNodes.
// they are disabled by default, but for this sample to work I need the player to be able to click on any tile.
// for your game you will have to decide when the best time would be to this or even which tiles would be
// best to enable. For example, you might only want to spawn new units around some building, so only
// enable the the tiles around the building so that the player cannot click on other tiles and disable
// the tiles whne yo uare done with them


foreach (TileNode n in map.nodes)
{
n.collider.enabled = true;
}

}


void Update()
{
// don;t do anything else if there was not a mouse click
if (!Input.GetMouseButtonUp(0)) return;

// cast a ray to check what the player "clicked" on. Only want to know
// about TILE clicks, so pass mask to check against layer for tiles only
Ray ray = rayCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 500f, tilesLayer))
{
// a tile gameobject was clicked on

// get the TileNode
node = hit.collider.GetComponent<TileNode>();

// sanity check
if (node == null) return;

// dont spawn here if there is alrelady a unit on this tile
if (node.units.Count > 0) return;

// send events to Playmaker
behavior.Fsm.Event("TileNodeActive");

//nodeId = node.idx;
//nodeLoc = node.transform.position;

// jump camera to the unit that was clicked on
camMover.Follow(node.transform);

}


}

void Spawn ()
{

if (node == null) return; // sanity check


Unit.SpawnUnit(unitFab, map, node);

behavior.Fsm.Event("UnitSpawned");

nodeLoc = node.transform.position;

}

}
« Last Edit: July 20, 2012, 11:58:27 AM by Lobohotpants »

Rlennox

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Tile Based Map and Navigation
« Reply #5 on: July 21, 2012, 06:07:18 AM »
I am trying to approach it from the other direction, by setting up a series of custom actions for the main methods and then using Playmaker FSM's as the game controller.

This is my first go at custom actions and i am also pretty new to c#, so to start i made a simple spawn unit action:

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Custom")]
[Tooltip("Spawns a mapnav unit")]

public class MapNavUnitSpawn : FsmStateAction
{
public GameObject unitFab;
public MapNav map;
public TileNode node;

public override void OnUpdate ()
{
Unit.SpawnUnit(unitFab, map, node);
}
}
}

This allows you to drag the unit prefab, mapnav and node into the state window to set what unit and where to spawn it. Tested it and it spawns the unit fine, except the FSM doesnt progress past this action. Tried using a FINISHED event and a custom event but no cigar.

Is there something i need to add to the action script?

The spawnunit method it calls is as follows:

Code: [Select]
// <summary>Instantiates a new unit on target node</summary>
public static NaviUnit SpawnUnit(GameObject unitFab, MapNav mapnav, TileNode node)
{
GameObject go = (GameObject)GameObject.Instantiate(unitFab);
go.transform.position = node.transform.position;
NaviUnit unit = go.GetComponent<NaviUnit>();
unit.mapnav = mapnav;
unit._tr = go.transform;
unit.LinkWith(node);
return unit;
}

Rlennox

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Tile Based Map and Navigation
« Reply #6 on: July 21, 2012, 06:24:04 AM »
Fixed one issue, need to put a check on the spawn so it doesnt spawn a unit every frame:

Code: [Select]
public override void OnUpdate ()
{
if (node.units.Count == 0)
{

Unit.SpawnUnit(unitFab, map, node);
}
}

Still doesnt progress past it though.

Rlennox

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Tile Based Map and Navigation
« Reply #7 on: July 21, 2012, 06:59:16 AM »
Realised that OnUpdate isnt where i want to handle this, way too tired, and then saw the Finish() function! Got it working now, code looks like this:

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Custom")]
[Tooltip("Spawns a mapnav unit")]

public class MapNavUnitSpawn : FsmStateAction
{
public GameObject unitFab;
public MapNav map;
public TileNode node;

public override void OnEnter ()
{
if (node.units.Count == 0)
{
Unit.SpawnUnit(unitFab, map, node);

}

Finish ();
}
}
}

That makes it progress to the next State when using a FINISHED transition.

Rlennox

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Tile Based Map and Navigation
« Reply #8 on: July 21, 2012, 06:43:36 PM »
Been tinkering a bit more, have updated the spawnunit action to allow using a variable to set the unit prefab, and an option to store the unit prefab. Then made a selectionindicator action and a radiusmarker action, that let you show a selection indicator at a gameobjects position, and a radiusmarker of a chosen size at a gameobjects.
« Last Edit: July 21, 2012, 09:34:59 PM by Rlennox »

Lobohotpants

  • Playmaker Newbie
  • *
  • Posts: 38
Re: Tile Based Map and Navigation
« Reply #9 on: July 22, 2012, 07:04:00 PM »
Well you have a much better grasp on C# than I do.  That's mostly why I went the script control route.  I see what you're doing though.  Good stuff.

Hoegbo

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Tile Based Map and Navigation
« Reply #10 on: September 30, 2012, 02:07:32 PM »
Oh I like this.

Do you have. Any more actions to share? For those code illiterate who can't tell a float from a goat :)

SeanC

  • Junior Playmaker
  • **
  • Posts: 61
Re: Tile Based Map and Navigation
« Reply #11 on: December 04, 2012, 08:42:16 PM »
Im upvoting this thread! The actions you guys have made are a great start! I think it would be wonderful to have some custom actions for these tools! It seems like there is a lot of people trying to find a solid tile-based movement solution and this tool really nails it well. If we could get a package of actions to dial into that would really open up a lot types of projects Playmaker can help build!

The actions you guys have made are a great start!

Sean
« Last Edit: December 04, 2012, 11:53:52 PM by SeanC »

tester

  • Playmaker Newbie
  • *
  • Posts: 31
Re: Tile Based Map and Navigation
« Reply #12 on: December 07, 2012, 08:54:45 AM »
upvote

murteas

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Tile Based Map and Navigation
« Reply #13 on: February 27, 2014, 11:39:41 AM »
Even though this post is old, I still think this is best place to talk about this unity addon.   I'm going to start working with the code,and hope we can get some more custom actions going. 

Redbeard

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Tile Based Map and Navigation
« Reply #14 on: March 03, 2014, 10:36:08 PM »
I am just starting to look into this package... anybody have any custom actions to share here?