Skip to content

Instantly share code, notes, and snippets.

@rabidgremlin
Last active June 19, 2021 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rabidgremlin/6a9a935facbcc4628121596a17e09326 to your computer and use it in GitHub Desktop.
Save rabidgremlin/6a9a935facbcc4628121596a17e09326 to your computer and use it in GitHub Desktop.
Walk and Look Code

Overview

This code was written for Fungus 2.x should work with the latest version but has not been tested. Also I was not happy with the abstract Walker class but it seemed to be the only way to get this to work at the time.

  • FaceDirectionCommand.cs - creates a new Fungus command block that sets the facing of a Walker
  • WalkToCommand.cs - creates a new Fungus command block that walks a Walker to a specific spot
  • Walker.cs - abstract class that implements the Walker logic
  • SpaceDudeWalker.cs - example implmentation of a Walker (for the space dude in LostInSpaaaace)

Usage

  1. For your character create a new script and add it to your character. The script should extend Walker NOT MonoBehaviour. Use SpaceDudeWalker.cs as an example.
  2. Add empty Gameobjects to your scene as targets to walk to
  3. Add your character to the scene
  4. In a fungus flowchart add the "Walk To" command. Set your character as the Walker and use one of the empty Gameobjects as the target.

And that is it.

Note: In LostInSpaaaace I generally used a Clickable sprite and the Object clicked event to trigger the walk (see here https://www.youtube.com/watch?v=-rtnk9OGCQY).

eg:

When object clicked:

  1. Used Walk To command to start chracter walking towards target placed near where clicked object was in scene
  2. Used Face Direction command to ensure character was facing the correct way
  3. Used Say command blocks to describe the clicked on object etc.
using UnityEngine;
using System.Collections;
using Fungus;
[CommandInfo("Walker", "Face Direction", "Turns the walker to face the specified direction.")]
public class FaceDirectionCommand : Command {
public enum Facing
{
Left,Right
}
public Walker walker;
public Facing turnToFace;
public override void OnEnter()
{
if (turnToFace == Facing.Left)
{
walker.FaceLeft();
}
if (turnToFace == Facing.Right)
{
walker.FaceRight();
}
Continue();
}
public override string GetSummary()
{
if (walker == null)
{
return "Error: No walker object selected";
}
return "Face " + walker.name + " " + turnToFace;
}
public override Color GetButtonColor()
{
return new Color32(216, 191,216, 255);
}
}
using UnityEngine;
using System.Collections;
using System;
public class SpaceDudeWalker : Walker {
public float speed = 5f;
private Animator anim;
private bool facingRight;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Debug.Log("Walk to Pint is:" + walkToPoint);
if (walkToPoint != null)
{
if (transform.position != walkToPoint)
{
transform.position = Vector3.MoveTowards(transform.position, walkToPoint, speed * Time.deltaTime);
}
else
{
anim.SetBool("Walking", false);
DoneWalk();
}
}
}
protected override void StartWalk()
{
if (walkToPoint.x < transform.position.x)
{
FaceLeft();
}
else
{
FaceRight();
}
anim.SetBool("Walking", true);
}
public override void FaceLeft()
{
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
public override void FaceRight()
{
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
}
using UnityEngine;
using System.Collections;
using Fungus;
public abstract class Walker : MonoBehaviour {
protected Vector3 walkToPoint;
protected Command callingCommand;
void Awake()
{
walkToPoint = transform.position;
}
public void WalkTo(Transform walkTo,Command caller)
{
if (callingCommand != null)
{
callingCommand.StopParentBlock();
callingCommand = null;
}
walkToPoint = walkTo.position;
StartWalk();
callingCommand = caller;
}
protected abstract void StartWalk();
public abstract void FaceLeft();
public abstract void FaceRight();
protected void DoneWalk()
{
if (callingCommand != null)
{
callingCommand.Continue();
callingCommand = null;
}
}
}
using UnityEngine;
using System.Collections;
using Fungus;
[CommandInfo("Walker","Walk To","Walks the walker to the specified target")]
public class WalkToCommand : Command {
[Tooltip("GameObject that we want to do the walking. Must have a Walker component.")]
public Walker walker;
[Tooltip("Target that we want the walker to walk to")]
public Transform target;
public override void OnEnter()
{
walker.WalkTo(target, this);
}
public override string GetSummary()
{
if (walker == null)
{
return "Error: No walker object selected";
}
if (target == null)
{
return "Error: No target object selected";
}
return "Walk " + walker.name + " to location of " + target.name;
}
public override Color GetButtonColor()
{
return new Color32(216, 191, 216, 255);
}
}
@thefnfplayer
Copy link

can you have a script that lets you look in 3D?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment