Search Unity

Continuous Cube Movement (Better Method To Stop?)

Discussion in 'Scripting' started by Deleted User, Oct 22, 2021.

  1. Deleted User

    Deleted User

    Guest

    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GridContinuousSlide : MonoBehaviour
    6. {
    7.     [Header("Layer Mask")]
    8.     public LayerMask environmentLayer;
    9.  
    10.     [Header("Player")]
    11.     public Rigidbody playerRigidbody;
    12.     public float playerSpeed = 15f;
    13.  
    14.     public bool Moving, canMoveU, canMoveD, canMoveL, canMoveR;
    15.  
    16.     private Vector3 playerDirection;
    17.  
    18.     private float playerRigidbodySpeed;
    19.  
    20.     private void Start()
    21.     {
    22.         playerRigidbody = GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         CheckEnvironmentCollision();
    28.  
    29.         // Monitor player speed
    30.  
    31.         playerRigidbodySpeed = playerRigidbody.velocity.magnitude;
    32.     }
    33.  
    34.     private void FixedUpdate()
    35.     {
    36.         if (Moving)
    37.         {
    38.             playerRigidbody.velocity = playerDirection * playerSpeed;
    39.  
    40.             // Check player speed (have we stopped?), if so set Moving to false
    41.  
    42.             if (playerRigidbodySpeed < 0.5f)
    43.                 Moving = false;
    44.         }
    45.  
    46.         if (!Moving)
    47.         {
    48.             if (canMoveU)
    49.             {
    50.                 if (Input.GetKey(KeyCode.UpArrow))
    51.                 {
    52.                     playerDirection = Vector3.forward;
    53.                     Moving = true;
    54.                 }
    55.             }
    56.  
    57.             if (canMoveD)
    58.             {
    59.                 if (Input.GetKey(KeyCode.DownArrow))
    60.                 {
    61.                     playerDirection = Vector3.back;
    62.                     Moving = true;
    63.                 }
    64.             }
    65.  
    66.             if (canMoveL)
    67.             {
    68.                 if (Input.GetKey(KeyCode.LeftArrow))
    69.                 {
    70.                     playerDirection = Vector3.left;
    71.                     Moving = true;
    72.                 }
    73.             }
    74.  
    75.             if (canMoveR)
    76.             {
    77.                 if (Input.GetKey(KeyCode.RightArrow))
    78.                 {
    79.                     playerDirection = Vector3.right;
    80.                     Moving = true;
    81.                 }
    82.             }
    83.         }
    84.     }
    85.  
    86.     private void CheckEnvironmentCollision()
    87.     {
    88.         if (Physics.Raycast(transform.position, Vector3.forward, 1f, environmentLayer))
    89.             canMoveU = false;
    90.         else
    91.             canMoveU = true;
    92.  
    93.         if (Physics.Raycast(transform.position, Vector3.back, 1f, environmentLayer))
    94.             canMoveD = false;
    95.         else
    96.             canMoveD = true;
    97.  
    98.         if (Physics.Raycast(transform.position, Vector3.left, 1f, environmentLayer))
    99.             canMoveL = false;
    100.         else
    101.             canMoveL = true;
    102.  
    103.         if (Physics.Raycast(transform.position, Vector3.right, 1f, environmentLayer))
    104.             canMoveR = false;
    105.         else
    106.             canMoveR = true;
    107.     }
    108. }
    109.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Deleted User likes this.