Forum rules - please read before posting.

Checking ActionPlayerLock from within custom scripts?

Firstly, I'm pretty new to all this, so please bear with me.

Prior to AC, I had developed a little character with it's own custom movement script(s). Unfortunately, this means the poor guy isn't affected when utilizing an ActionList's "Lock" function during cutscenes and such.

Here is [what I believe to be] the most relevant part of my movement script (in FixedUpdate):

   if(Mathf.Abs(_rb.velocity.x) < _speed)
    {
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            _rb.AddForce(Vector2.right * 50f);
        }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            _rb.AddForce(- Vector2.right * 50f);
        }
    }

Is there any way to wrap this in an 'if' statement to see if AC has set the lock? Such as "if(AC.lock == false){movement}" or something like that. I've looked around the different ActionLock files, but haven't been able to integrate it yet.

Thanks for any help, and happy holidays!

Cory

Comments

  • You can read the Player script's AllDirectionsLocked() function, or individual properties such as downMovementLocked:

    if (AC.KickStarter.player.AllDirectionsLocked ())
    {
        return;
    }
    
  • Would you be willing to explain a bit further? I really do appreciate any support or a point in the right direction! My coding knowledge is next to minimal... so it's definitely a me problem and not the explanation, lol.

    I've tried that code in various placements and variations, but to no avail. So far I'm getting either no affect or receive errors. Where should I be utilizing it, and does it need any additional code elsewhere?

    Here is my full file (sorry for splitting it up between quote/code, the forum was being finicky). Essentially, I want the 'if' statement in the FixedUpdate to only work when 'AllDirectionsLocked' reads false.

    Controller.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [RequireComponent(typeof(Rigidbody2D))]
    public class Controller : MonoBehaviour
    {
    public float _speed = 1f;
    private Rigidbody2D _rb;

    void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
    }
    
    
    void FixedUpdate()
    {
            if(Mathf.Abs(_rb.velocity.x) < _speed)
                {
                    if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
                    {
                        _rb.AddForce(Vector2.right * 50f);
                    }
                    if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
                    {
                        _rb.AddForce(- Vector2.right * 50f);
                    }
                }
    }   
    
    void Update () {
        transform.position = new Vector3(
        Mathf.Clamp(transform.position.x, -15f, 12f),
        transform.position.y,
        transform.position.z);
    }
    

    }

  • No problem - give this a try:

    using UnityEngine;
    
    [RequireComponent (typeof (Rigidbody2D))]
    public class Controller : MonoBehaviour
    {
    
        public float _speed = 1f;
        private Rigidbody2D _rb;
    
        void Start()
        {
            _rb = GetComponent<Rigidbody2D> ();
        }
    
        void FixedUpdate ()
        {
            if (Mathf.Abs (_rb.velocity.x) < _speed)
            {
                if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow))
                {
                    if (!AC.KickStarter.player.rightMovementLocked)
                    {
                        _rb.AddForce (Vector2.right * 50f);
                    }
                }
                if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow))
                {
                    if (!AC.KickStarter.player.leftMovementLocked)
                    {
                        _rb.AddForce (-Vector2.right * 50f);
                    }
                }
            }
        }
    
        void Update ()
        {
            transform.position = new Vector3
                (Mathf.Clamp(transform.position.x, -15f, 12f),
                transform.position.y,
                transform.position.z);
        }
    
    }
    

    This amendment separates the movement locks - so you can use the Player: Constrain Action to lock left and right movement individually with your custom movement script.

  • Aha! It worked. Your solution is much more straight forward than anything I had come up with (that none worked, lol). Thanks, again.

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.