Forum rules - please read before posting.

Walk to NPC - but move only on X-axis?

Hi

I'm making a 2D sidescroller game where the player is limited to movement on the X-axis.
I've achieved this by constraining his movement in a start-up actionlist.

This works fine, but for one issue.

When I use the "walk to" feature (the "player action" you can set on interaction with an object/NPC), the player does not keep to the X-axis but moves on the Y-axis too to get to the object/NPC.

I know I can solve this by using the player action "walk to marker" instead, but this is not satisfying for me, as that looks silly in many instances when a player then walks past an object or NPC to get to the "walk to" marker that might be placed on the other side of the object from the current player position (placing the "walk to" marker directly under the object/NPC is a no-go, as I don't want my player and object/NPC sprites overlapping during the interaction).

Another solution I can think of, is to have 2 walk-to markers for each object/NPC, and have the player walk to the nearest one, but I see no option to do that.

Is there a way to limit the player movemt to the X-axis ALSO when he performs walk-to actions?

I've tried setting up a navmesh and making it real thin, but the player completely ignores this.

The behaviour is the same when using Brain2D prefab, by the way.

Comments

  • edited September 2020

    Sounds like the best thing to do is just have the one Hotspot Walk-to Marker, but reposition it either side of the NPC based on the Player's position.

    If you make the Marker a child of the Hotspot, you can do this by changing its local x position only.

    Here's a sample script, that would be attached to the Hotspot:

    using UnityEngine;
    using AC;
    
    public class RepositionHotspotMarker : MonoBehaviour
    {
    
        [SerializeField] private float localLeftPosition = -0.1f;
        [SerializeField] private float localRightPosition = 0.1f;
    
        private void OnEnable ()
        {
            EventManager.OnHotspotInteract += OnInteract;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotInteract -= OnInteract;
        }
    
        private void OnInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                bool playerOnLeft = (KickStarter.player.transform.position.x < transform.position.x);
                Vector3 localMarkerPosition = hotspot.walkToMarker.transform.localPosition;
                localMarkerPosition.x = (playerOnLeft) ? localLeftPosition : localRightPosition;
                hotspot.walkToMarker.transform.localPosition = localMarkerPosition;
            }
        }
    
    }
    

    When the Hotspot is interacted with, it'll give its Marker a new local position (either using Local Left Position or Local Right Position as its x-value) before the Player walks there.

  • Hi Chris

    Yes, that is a really good and simple solution.
    Thanks!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.