Forum rules - please read before posting.

Water current/moving walkway/etc effect?

In a 2D game, I've created a river that is supposed to flow from left to right. The player is able to craft a raft which, when used on the river, changes the player sprite to include the raft. It also switches the navmesh so they can only "walk" on the river. If they stand still, they are dragged to the right by the current. If they move left, they move slowly because they are rowing against the current. If they click to move right, they move faster.

I was thinking about parenting the player to an invisible game object that slowly moves to the right, but I'm not entirely sure this would work (and I'm a bit nervous about a solution where an object could theoretically get too far away from origin and cause issues). Any suggestions that would play well with the AC movement system?

Comments

  • Best to manipulate the Player's root position directly.

    If it's a straight line, then you can just use a simple script to move the Player's Transform position by some amount if they're idle.

    If they're pathfinding, you can compare their current position and target position's x values to determine which direction they're moving, and update their movement speed accordingly.

    Here's a script that should do it. To enable / disable the effect, use the Object: Send message Action to pass the Turn On and Turn Off messages to it:

    using UnityEngine;
    using AC;
    
    public class BoatMovement : MonoBehaviour
    {
    
        public bool isOnBoat;
    
        public float dragSpeed = 1f;
        public float maxDragPosition = 5f;
    
        public float leftSpeed = 0.5f;
        public float rightSpeed = 2f;
    
        private float idleTimeBeforeDrag = 1f;
        private float idleTimer;
    
        void OnEnable ()
        {
            EventManager.OnCharacterEndPath += OnCharacterEndPath;
            EventManager.OnEnterGameState += OnEnterGameState;
        }
    
        void OnDisable ()
        {
            EventManager.OnCharacterEndPath -= OnCharacterEndPath;
            EventManager.OnEnterGameState -= OnEnterGameState;
        }
    
        public void TurnOn ()
        {
            isOnBoat = true;
        }
    
        public void TurnOff ()
        {
            isOnBoat = false;
        }
    
        void OnCharacterEndPath (Char character, Paths path)
        {
            if (character == KickStarter.player) idleTimer = 0f;
        }
    
        void OnEnterGameState (GameState gameState)
        {
            if (gameState == GameState.Normal) idleTimer = 0f;
        }
    
        void Update ()
        {
            if (!isOnBoat)
            {
                return;
            }
    
            if (KickStarter.player.charState == CharState.Idle)
            {
                idleTimer += Time.deltaTime;
                if (idleTimer >= idleTimeBeforeDrag && KickStarter.player.Transform.position.x < maxDragPosition)
                {
                    Vector3 positionOffset = Vector3.right * dragSpeed * Time.deltaTime;
                    KickStarter.player.Transform.position += positionOffset;
                }
                return;
            }
    
            idleTimer = 0f;
            if (KickStarter.player.GetTargetPosition ().x > KickStarter.player.Transform.position.x)
            {
                KickStarter.player.walkSpeedScale = rightSpeed;
            }
            else
            {
                KickStarter.player.walkSpeedScale = leftSpeed;
            }
        }
    
    }
    
  • Thank you, Chris! It wasn't originally working as intended (the character would be dragged, stop, be dragged again, etc), but if you make this change, it works perfectly:

    private float idleTimeBeforeDrag = 0f;

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.