Forum rules - please read before posting.

Change motion control ACTION - BUG

Hey!
So since I need to use "Character : Move To Point" Action, but I am using a Manual motion control on my player. So I did a simple script, that changes Motion Control to Automatic or Manual:

override public float Run() {
GameObject player = KickStarter.player.gameObject;
if(manual) {
KickStarter.player.motionControl = MotionControl.Manual;
}
else {
KickStarter.player.motionControl = MotionControl.Automatic;
}
return 0f;
}

override public void ShowGUI() {
            manual = EditorGUILayout.Toggle("Is Manual?", manual);
            AfterRunningOption();
        }

Then I used this action to change Motion Control to Automatic, when I want the character to "move to point/marker", and then turn back to Manual (as you can see in the picture).

But there is a PROBLEM. When the player Motion Control switches to Automatic, the player sprite changes its x scale somehow (image), and the gravity scale go to 1, although I set it on 2, on my Player prefab Rigidbody2D.


Thanks for the answer!
Viktor Stopka

Comments

  • edited September 2019

    First of all, while a custom Action can indeed set the motion control value when desired - you can also rely on custom events to change this for you whenever the characer begins and ends a path.

    Something like this would do it:

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class AutomaticMotionWhenPathfinding : MonoBehaviour
    {
    
        private Char character;
    
        private void OnEnable ()
        {
            character = GetComponent <Char>();
            EventManager.OnCharacterSetPath += OnSetPath;
            EventManager.OnCharacterEndPath += OnEndPath;
        }
    
        private void OnDisable ()
        {
            EventManager.OnCharacterSetPath -= OnSetPath;
            EventManager.OnCharacterEndPath -= OnEndPath;
        }
    
        private void OnSetPath (Char character, Paths path)
        {
            if (this.character == character)
            {
                character.motionControl = MotionControl.Automatic;
            }
        }
    
        private void OnEndPath (Char character, Paths path)
        {
            if (this.character == character)
            {
                character.motionControl = MotionControl.Manual;
            }
        }
    
    }
    

    As for the actual issue, let's see your character's full Inspector - all components on the root object.

    the player sprite changes its x scale somehow (image)

    Is the character's scale changing, or is it that they are rotating on the Z-axis? Try unchecking Turn root object in 3D? in their Inspector. This will only be visible when their Motion control is set to Automatic and they use Sprites Unity or Sprites Unity Complex for their animation engine. Set to Automatic to show the field, uncheck it, then set back to Manual as desired.

    the gravity scale go to 1, although I set it on 2, on my Player prefab Rigidbody2D.

    AC will control the gravity value when Motion control is set to Automatic, though I agree it should default to the original value set in the Inspector, not 1. I shall correct this in the next update, though you may also be able to get around this by having it always set to 1, and increasing the mass instead.

    Be aware also, that it's not strictly necessary to switch back to Automatic motion control whenever you want the player to move somewhere. If they have been instructed to move along a path, but they're in manual control, you can read their GetTargetPosition () function to use as the basis for your own movement.

    i.e.:

    private void Update ()
    {
        if (KickStarter.player.activePath != null)
        {
            // The player is attached to a Path
            Vector3 targetPosition = GetTargetPosition (true);
    
            // Insert code to move towards targetPosition here
        }
    }
    

    How you actually move the character would be down to your script, but this would bypass the need to switch to Automatic motion control. A tutorial on this topic can be found here.

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.