Forum rules - please read before posting.

2D Platformer with AC?

Has anyone here gotten a 2d-platformer style game to work with AC?

So the thing is we find the functionality of cutscenes, dialogue, etc. to be something we want in our game, but we want the navigation method to be something from a custom script we created
any help is appreciated, thank you!

Comments

  • On a side note -- it looks like this portion of AC is failing -- seems like I cannot import

    using AC;

    into my scripts -- might anyone happen to know why this may be?

  • Ah ignore the above -- looks like it was an issue with the .asmdef file.

  • For robust platforming mechanics, you'll need a separate asset / script to handle the movement - IIRC this was broadly how Lair of the Clockwork God was made.

    Setting your Player's Motion control field to Manual will have AC give up control over their Transform, in favour of another asset / custom script.

    For details on how to incorporate a third-party controller, see the Manual's "Custom motion controllers" chapter.

  • So we tried creating our own script for player movement below:

    using UnityEngine;
    using AC;

    public class PlayerMovement : MonoBehaviour
    {
    public float velX = 10;
    public float maxRange = 1;
    public float maxHeight = 3;
    public float heightMultiplier = 1.2f;
    public float FallMultiplier = 1.6f;
    public float flinchTimer = 2;
    float flinchTimeLeft;
    float timeOfFlight;
    float globalGravity;
    float velY;
    bool toJump;
    bool isJumping;
    float move;
    public LayerMask layerMask;
    private Rigidbody2D rb;
    private BoxCollider2D bc;

    string[] dangerTags = { "Bullet", "Enemy" };
    
    private void Awake()
    {
    
        rb = GetComponent<Rigidbody2D>();
        bc = GetComponent<BoxCollider2D>();
    
        if (rb == null)
        {
            // Handle the absence of Rigidbody2D (e.g., log a warning)
            Debug.LogWarning("Rigidbody2D is not attached to this GameObject.");
        }
    
        timeOfFlight = maxRange / velX;
        globalGravity = 2 * maxHeight / Mathf.Pow(timeOfFlight, 2);
        rb.gravityScale = globalGravity;
        velY = globalGravity * timeOfFlight;
        // Debug.LogFormat("timeofflight: {0}, gravity: {1}, velY: {2}", timeOfFlight, globalGravity, velY);
        flinchTimeLeft = 0;
    
    }
    
    void FixedUpdate()
    {
        //don't allow player to move if cutscene is running
        if (AC.GlobalVariables.GetVariable("cutsceneRunning").BooleanValue)
        {
            return;
        }
    
        Debug.LogFormat("{0},{1},{2}", move, isJumping, toJump);
    
        Vector2 vel = rb.velocity;
        vel.x = move * velX;
    
        if (toJump && IsGrounded())
        {
            vel.y = velY;
            isJumping = true;
        }
    
        if (isJumping && toJump)
        {
            rb.gravityScale = globalGravity / heightMultiplier;
        }
        else
        {
            isJumping = false;
            rb.gravityScale = globalGravity;
        }
    
        if (flinchTimeLeft <= 0)
        {
            rb.velocity = vel;
        }
        else
        {
            flinchTimeLeft -= Time.fixedDeltaTime;
        }
    
        if (rb.velocity.y < 0.0f)
        {
            rb.gravityScale = FallMultiplier * globalGravity;
        }
    }
    
    // Update is called once per frame
    public void Update()
    {
        move = Input.GetAxisRaw("Horizontal");
        toJump = Input.GetAxisRaw("Vertical") > 0.3f;
    }
    
    void OnTriggerEnter2D(Collider2D other)
    {
        Vector2 flinch = new Vector2(-velX, velY);
    
        for (int i = 0; i < dangerTags.Length; i++)
        {
            if (other.CompareTag(dangerTags[i]))
            {
                rb.AddForce(flinch, ForceMode2D.Impulse);
                Flinch();
            }
    
            Debug.LogFormat("Game Tag: {0}", other.gameObject.tag);
            if (other.gameObject.tag == "Bullet")
            {
                Destroy(other.gameObject);
            }
        }
    }
    void OnCollisionEnter2D(Collision2D other)
    {
        Vector2 flinch = new Vector2(-velX, velY);
    
        for (int i = 0; i < dangerTags.Length; i++)
        {
            if (other.gameObject.CompareTag(dangerTags[i]))
            {
                rb.AddForce(flinch, ForceMode2D.Impulse);
                Flinch();
            }
        }
    }
    
    private bool IsGrounded()
    {
        float extraDx = 0.01f;
        return Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down, extraDx, layerMask);
    }
    
    public void Flinch()
    {
        flinchTimeLeft = flinchTimer;
    }
    

    }

    The issue is that the jump movement is not being recognized? The left and right are working fine, but we're thinking that something in AC may be overriding the velocity.

    Might you happen to know why this may be / how we can change it?

  • I set motion control to manual just as a heads up -- but sadly it seems like it still didn't work.

  • I'll need to see more to attempt a recreation - can you share a screenshot of your Player's full root Inspector?

  • Also on a sidenote -- would pathfinding work on something like a tileset where we only have 2 directions for the character (moving left and right)?

    I wasn't sure how to place the navmesh on the tiles so that only the top-parts are walkable.

  • I'm not getting any difference in the custom movement when AC's Player component is attached. Is the behaviour correct for you when you don't involve AC?

    Quick note re: cutscene checking: you don't need to rely on a variable for this, you can directly read:

    AC.KickStarter.stateHandler.IsInCutscene ()
    

    would pathfinding work on something like a tileset where we only have 2 directions for the character (moving left and right)?

    A custom script that limits pathfinding to the X-axis can be found here:

    https://adventure-creator.fandom.com/wiki/2D_point_and_click_along_one_axis

  • Thanks for getting back to me -- so just to make sure I'm understanding correctly, are you saying that you are able to jump as well?

    And ah yes -- without AC the behavior is fine -- we tested out a few things and and narrowed it down to the "GameEngine" gameObject that is created when initializing a scene with AC. So when this object is present, it looks like jumping is not working. However, it looks like it's needed for the dialogue to work.

    Thank you so much! Just wondering -- is there a way we can generate a navmesh for a tileset map in a scene?

    Thank you!

  • Thanks for getting back to me -- so just to make sure I'm understanding correctly, are you saying that you are able to jump as well?

    I mat not have had the right values / scale, so the jumping didn't look very natural - but it did move upward both with AC present and absent.

    we tested out a few things and and narrowed it down to the "GameEngine" gameObject that is created when initializing a scene with AC.

    The GameEngine object is AC itself. What are your Unity / AC versions, and what is your Settings Manager's Movement method field set to? Try setting this to None, so that AC doesn't do anything regarding Player movement.

    is there a way we can generate a navmesh for a tileset map in a scene?

    AC's built-in 2D pathfinding relies on Polygon Collider 2D components, while Tilemaps generate Tilemap Collider 2Ds. It may be possible to write a script that uses the shape data from one to control another. I'm not 100% but I'll take a look.

  • edited November 2023

    @ChrisIceBox

    My movement is set to None.

    I mat not have had the right values / scale, so the jumping didn't look very natural - but it did move upward both with AC present and absent.

    Can I ask what you might have done / what your setup looks like?

  • Also is it possible for you to send a video of how it looks and what your hierarchy / values are?

  • I added the script component to a simple box sprite in the scene, added a ground sprite underneath, and used the same values as in your screenshot.

    The movement was very fast and a bit jerky, but that might just be down to the scale of the sprites/scene. The key thing is that there was no difference once I used the Scene Manager to convert the scene into an AC one.

    What are your AC/Unity versions?

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.