Forum rules - please read before posting.

Making player character slide along a surface (move towards)

edited September 2023 in Technical Q&A

I was looking into making a custom action that would trigger a simple move towards function that moves a game object or player towards a target and stops on arrival. it seems simple enough, but there is a bunch of advanced stuff that takes me ages to finish. Like keeping the action running until I tell it to stop at the desired position.
the built in object transform action does not work on characters, even if it does allow you to move the player character (it does nothing) But when I add a simple move towards script to the player, it moves to the desires position quite happily without issues.

Comments

  • edited September 2023
    // Declare variables here
        public Transform _target;
        public Transform _ObjectToMove;
        public float _speed = 1.0f;
    
        public override float Run()
        {
            /* 
             * This function is called when the action is performed.
             * 
             * The float to return is the time that the game
             * should wait before moving on to the next action.
             * Return 0f to make the action instantenous.
             * 
             * For actions that take longer than one frame,
             * you can return "defaultPauseTime" to make the game
             * re-run this function a short time later. You can
             * use the isRunning boolean to check if the action is
             * being run for the first time, eg: 
             */
            if (_target != null)
            {
                isRunning = true;
    
                // Check if the position of the object to move and the target are approximately equal.
                if (Vector3.Distance(_ObjectToMove.position, _target.position) < 0.001f)
                {
                    isRunning = false;
                    return 0f;
                }
                else
                {
                    // Move our position a step closer to the target.
                    var step = _speed * Time.deltaTime; // calculate distance to move
                    _ObjectToMove.position = Vector3.MoveTowards(_ObjectToMove.position, _target.position, step);
    
                    return defaultPauseTime;
                }
            }
            else
            {
                isRunning = false;
                return 0f;
            }
        }
    
  • edited September 2023

    I actually managed to get it working. It is still rough, but a start. As this setup does not allow for easing, I will probably figure out a better way, but it is a start

  • Object: Transform can work with characters, provided they have a Moveable component and aren't moving at the time. If they move with a Rigidbody, you'll also need to check Move With Rigidbody in the Moveable component's Inspector.

    To apply smoothing to your custom Action as they approach some threshold, you can multiply the step by a factor that decreases within this threshold:

    // Declare variables here
    public Transform _target;
    public Transform _ObjectToMove;
    public float _speed = 1.0f;
    float easeThreshold = 1f; // How close to begin slowing down
    
    public override float Run()
    {
        if (_target == null) return 0f;
    
        isRunning = true;
    
        float distance = Vector3.Distance (_ObjectToMove.position, _target.position);
    
        // Check if the position of the object to move and the target are approximately equal.
        if (distance < 0.001f)
        {
            isRunning = false;
            return 0f;
        }
    
        // Move our position a step closer to the target.
        var step = _speed * Time.deltaTime; // calculate distance to move
    
        if (distance < easeThreshold)
        {
            step *= Mathf.Clamp (distance / easeThreshold, 0.1f, 1f);
        }
    
        _ObjectToMove.position = Vector3.MoveTowards(_ObjectToMove.position, _target.position, step);
    
        return defaultPauseTime;
    }
    
  • Thanks! This scene is shaping up to be very satisfying.

  • oh yes here is the character move action in... action:
    https://youtu.be/zCyIeasvoTI

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.