Continuous Cube Movement (Better Method To Stop?)

Hello,

I’m trying to replicate the sliding movement as seen in games like roller splat :

On a cube with a rigid body (Y Pos locked, and Rotation X, Y, Z locked) I have the following code. (pasted below).

There is a few tutorials on YouTube regarding this game, but mostly deal with swipe functionality, I just want to do it with standard keyboard controls. I have the basic functionality working, but was wondering if someone could take a look at my movement code, and tell me if everything seems fine? For instance, I’m detecting the rigid body velocity speed to determine if I have ‘stopped’, I’m not sure this is the best way to achieve this, is there a better way / “safer” way I should be determining this ? How about the rest of the code, does it make sense ( it works, but am I doing anything ‘stupid’ here? ). Thanks in advance.

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

public class GridContinuousSlide : MonoBehaviour
{
    [Header("Layer Mask")]
    public LayerMask environmentLayer;

    [Header("Player")]
    public Rigidbody playerRigidbody;
    public float playerSpeed = 15f;

    public bool Moving, canMoveU, canMoveD, canMoveL, canMoveR;

    private Vector3 playerDirection;

    private float playerRigidbodySpeed;

    private void Start()
    {
        playerRigidbody = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        CheckEnvironmentCollision();

        // Monitor player speed

        playerRigidbodySpeed = playerRigidbody.velocity.magnitude;
    }

    private void FixedUpdate()
    {
        if (Moving)
        {
            playerRigidbody.velocity = playerDirection * playerSpeed;

            // Check player speed (have we stopped?), if so set Moving to false

            if (playerRigidbodySpeed < 0.5f)
                Moving = false;
        }

        if (!Moving)
        {
            if (canMoveU)
            {
                if (Input.GetKey(KeyCode.UpArrow))
                {
                    playerDirection = Vector3.forward;
                    Moving = true;
                }
            }

            if (canMoveD)
            {
                if (Input.GetKey(KeyCode.DownArrow))
                {
                    playerDirection = Vector3.back;
                    Moving = true;
                }
            }

            if (canMoveL)
            {
                if (Input.GetKey(KeyCode.LeftArrow))
                {
                    playerDirection = Vector3.left;
                    Moving = true;
                }
            }

            if (canMoveR)
            {
                if (Input.GetKey(KeyCode.RightArrow))
                {
                    playerDirection = Vector3.right;
                    Moving = true;
                }
            }
        }
    }

    private void CheckEnvironmentCollision()
    {
        if (Physics.Raycast(transform.position, Vector3.forward, 1f, environmentLayer))
            canMoveU = false;
        else
            canMoveU = true;

        if (Physics.Raycast(transform.position, Vector3.back, 1f, environmentLayer))
            canMoveD = false;
        else
            canMoveD = true;

        if (Physics.Raycast(transform.position, Vector3.left, 1f, environmentLayer))
            canMoveL = false;
        else
            canMoveL = true;

        if (Physics.Raycast(transform.position, Vector3.right, 1f, environmentLayer))
            canMoveR = false;
        else
            canMoveR = true;
    }
}

That’s almost certainly just a lerp/tween with elastic easing. No physics involved.

1 Like