FANDOM


This script allows for 2D point-and-click movement along the X-axis only - with no need for NavMeshes or pathfinding.

It works by taking the X position of the mouse (in world-space) and the Y position of the player, and having the player walk to that vector.

To use it:

  1. Set your game's Movement method to Point And Click, and your Camera perspective to 2D.
  2. Disable the built-in movement system with the Engine: Manage systems Action, setting Movement to Disabled.  This can be done either in your scene's OnStart Cutscene, or your game's ActionList on start game ActionList.
  3. Paste thes code below into a C# script named PointClickHorizontal2D, and attach it to any object in your scene
  4. (Optional) Tweak the Horizontal Limits field in the component's Inspector to set how far the player can move along the X-axis.

PointClickHorizontal2D.cs

using UnityEngine;
using System.Collections;
using AC;

public class PointClickHorizontal2D : MonoBehaviour
{

public Vector2 horizontalLimits = new Vector2 (-100f, 100f);

private void Update ()
{
if (KickStarter.stateHandler.IsInGameplay ())
{
if (KickStarter.playerInput.GetMouseState () == MouseState.SingleClick)
{
// Get the mouse position in world space
float zDepth = KickStarter.player.transform.position.z - Camera.main.transform.position.z;
Vector3 mouseScreenPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, zDepth);
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint (mouseScreenPosition);

// Limit the mouse position to within horizontalLimits
if (mouseWorldPosition.x < horizontalLimits.x)
{
mouseWorldPosition.x = horizontalLimits.x;
}
if (mouseWorldPosition.x > horizontalLimits.y)
{
mouseWorldPosition.x = horizontalLimits.y;
}

// Create a vector with the mouse's x, and the player's y
Vector2 clickPoint = new Vector2 (mouseWorldPosition.x, KickStarter.player.transform.position.y);

// Move the player to that point
KickStarter.player.MoveToPoint (clickPoint, false, false);
}
}
}

}
Community content is available under CC-BY-SA unless otherwise noted.