Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Slippery 2D Movement Not Working

Discussion in 'Physics' started by Typods, Apr 23, 2021.

  1. Typods

    Typods

    Joined:
    Feb 27, 2018
    Posts:
    55
    Hi, so I'm trying to make a slippery movement script to simulate underwater movement from a side perspective. I was following a tutorial video simulating something similar and wrote this code but when I tested it, it didn't do anything. I added some debug.logs to see where the problem lied, but they all worked, so I guess I'm confused at what's not correct. If you have any insight on this or alternatives to programming this I would love some assistance! I would like to say that I am still fairly new to Unity and C#. Thank you so much in advance!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SquidMovement : MonoBehaviour
    6. {
    7.     //Several variables
    8.     public Rigidbody2D rb;
    9.     Vector2 movement, movementOrder;
    10.     bool moveW = false, moveA = false, moveS = false, moveD = false;
    11.     float fSpeed = 0.5f, fMaxSpeed = 2.0f;
    12.  
    13.  
    14.     //Getting controls from the player and updating the order
    15.     private void Update()
    16.     {
    17.  
    18.         if(Input.GetKeyDown(KeyCode.W))
    19.         {
    20.             moveW = true;
    21.             Debug.Log("Pressed W");
    22.         }
    23.  
    24.         if(Input.GetKeyDown(KeyCode.A))
    25.         {
    26.             moveA = true;
    27.         }
    28.  
    29.         if(Input.GetKeyDown(KeyCode.S))
    30.         {
    31.             moveS = true;
    32.         }
    33.  
    34.         if(Input.GetKeyDown(KeyCode.D))
    35.         {
    36.             moveD = true;
    37.         }
    38.  
    39.  
    40.         if (Input.GetKeyUp(KeyCode.W))
    41.         {
    42.             moveW = false;
    43.             Debug.Log("LetGoOf W");
    44.         }
    45.  
    46.         if (Input.GetKeyUp(KeyCode.A))
    47.         {
    48.             moveA = false;
    49.         }
    50.  
    51.         if (Input.GetKeyUp(KeyCode.S))
    52.         {
    53.  
    54.             moveS = false;
    55.         }
    56.  
    57.         if (Input.GetKeyUp(KeyCode.D))
    58.         {
    59.             moveD = false;
    60.         }
    61.     }
    62.  
    63.     //Executing physics calculations
    64.     private void FixedUpdate()
    65.     {
    66.  
    67.         //movementOrder = new Vector2(0, 0);
    68.         if (moveW && !moveS)
    69.         {
    70.             movementOrder.y = fMaxSpeed;
    71.             Debug.Log("Did something");
    72.         }
    73.  
    74.         else if (moveS && !moveW)
    75.         {
    76.             movementOrder.y = -fMaxSpeed;
    77.         }
    78.  
    79.         else if (moveS && moveW)
    80.         {
    81.             movementOrder.y = 0;
    82.         }
    83.  
    84.         if (moveD && !moveA)
    85.         {
    86.             movementOrder.x = fMaxSpeed;
    87.         }
    88.  
    89.         else if (moveA && !moveD)
    90.         {
    91.             movementOrder.x = -fMaxSpeed;
    92.         }
    93.  
    94.         else if (moveA && moveD)
    95.         {
    96.             movementOrder.x = 0;
    97.         }
    98.  
    99.  
    100.         //Executing Order
    101.         if (moveW && movement.y < movementOrder.y)
    102.         {
    103.             movement.y += fSpeed * Time.fixedDeltaTime;
    104.             Debug.Log("The Other Thing");
    105.         }
    106.  
    107.         if (moveS && movement.y > movementOrder.y)
    108.         {
    109.             movement.y -= fSpeed * Time.fixedDeltaTime;
    110.         }
    111.  
    112.         if (moveA && movement.x > movementOrder.x)
    113.         {
    114.             movement.x -= fSpeed * Time.fixedDeltaTime;
    115.         }
    116.  
    117.         if (moveD && movement.x < movementOrder.x)
    118.         {
    119.             movement.x += fSpeed * Time.fixedDeltaTime;
    120.         }
    121.  
    122.  
    123.  
    124.     }
    125.  
    126.  
    127.  
    128.  
    129. }
    130.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,594
    It won't. Modifying a field called "movement" won't actually cause movement. It's just a field. You're not telling the Rigidbody2D to do anything.
     
  3. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    128
    Can you show us what kind of movement it should be ? What it should look like.