Forum rules - please read before posting.

Constrain (2d) NPC and Player to X axis via Action List

edited October 2023 in Technical Q&A

Hello. 
As the title suggests, I'd like to know what the best way to contrain player and npc movement to the X axis (left and right only via an action list. 
 
For context, I'm trying to showcase a mechanic in the game, but because I lack UP/Down directions for some of the animations, the characters don't line up with each other, and it appears inconsistent. Is it possible to do this with the resources available, or does it require scripting? If so, are there any scripts available and instructions or tutorials on how to use them?

As always, thank you in advance!

Comments

  • Set your characters Turn speed values to -1 to have them turn instantly - this will help them stick to a fixed point on the Y-axis if both they and the target have the same Y-position.

    What movement method does your game use?

    If you're using Direct, you can use the Player: Constrain Action to prevent up/down movement.

    If you're using Point And Click, this script on the wiki will limit the Player's movement to one axis.

  • I'm using point and click method. I just need something to constrain the characters when I use the shotgun (for example) because I don't yet have all of the animations for shooting and for being shot (up,down). Thank you for the script. I'm looking into it now.

  • Final dumb question I could never figure out how to do. I've set the script on the characters and I've disabled it. Can it be enabled via Action List? Can I use "Send Message" for example?

  • The Engine: Call event Action can be used to affect its "enabled" state, but it requires both the ActionList and the character be in the scene file.

    To have it work across prefab/asset files, you'll need to modify the script by adding a bool you can affect:

    using UnityEngine;
    using AC;
    
    public class PointClickHorizontal2D : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnCharacterSetPath += OnCharacterSetPath; }
        private void OnDisable () { EventManager.OnCharacterSetPath -= OnCharacterSetPath; }
    
        public bool isOn;
    
        public void TurnOn () { isOn = true; }
        public void TurnOff () { isOn = false; }
    
        private void OnCharacterSetPath (AC.Char character, Paths path)
        {
            if (character == KickStarter.player && isOn)
            {
                if (path.nodes.Count > 1)
                {
                    path.nodes.RemoveRange (0, path.nodes.Count - 2);
                }
                Vector3 lastNode = path.nodes[path.nodes.Count-1];
                lastNode.y = character.transform.position.y;
                path.nodes[path.nodes.Count-1] = lastNode;
            }
        }
    
    }
    

    You can then use the Object: Send message Action's Turn On / Turn Off commands.

  • thank you! as always.

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.