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!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquidMovement : MonoBehaviour
{
//Several variables
public Rigidbody2D rb;
Vector2 movement, movementOrder;
bool moveW = false, moveA = false, moveS = false, moveD = false;
float fSpeed = 0.5f, fMaxSpeed = 2.0f;
//Getting controls from the player and updating the order
private void Update()
{
if(Input.GetKeyDown(KeyCode.W))
{
moveW = true;
Debug.Log("Pressed W");
}
if(Input.GetKeyDown(KeyCode.A))
{
moveA = true;
}
if(Input.GetKeyDown(KeyCode.S))
{
moveS = true;
}
if(Input.GetKeyDown(KeyCode.D))
{
moveD = true;
}
if (Input.GetKeyUp(KeyCode.W))
{
moveW = false;
Debug.Log("LetGoOf W");
}
if (Input.GetKeyUp(KeyCode.A))
{
moveA = false;
}
if (Input.GetKeyUp(KeyCode.S))
{
moveS = false;
}
if (Input.GetKeyUp(KeyCode.D))
{
moveD = false;
}
}
//Executing physics calculations
private void FixedUpdate()
{
//movementOrder = new Vector2(0, 0);
if (moveW && !moveS)
{
movementOrder.y = fMaxSpeed;
Debug.Log("Did something");
}
else if (moveS && !moveW)
{
movementOrder.y = -fMaxSpeed;
}
else if (moveS && moveW)
{
movementOrder.y = 0;
}
if (moveD && !moveA)
{
movementOrder.x = fMaxSpeed;
}
else if (moveA && !moveD)
{
movementOrder.x = -fMaxSpeed;
}
else if (moveA && moveD)
{
movementOrder.x = 0;
}
//Executing Order
if (moveW && movement.y < movementOrder.y)
{
movement.y += fSpeed * Time.fixedDeltaTime;
Debug.Log("The Other Thing");
}
if (moveS && movement.y > movementOrder.y)
{
movement.y -= fSpeed * Time.fixedDeltaTime;
}
if (moveA && movement.x > movementOrder.x)
{
movement.x -= fSpeed * Time.fixedDeltaTime;
}
if (moveD && movement.x < movementOrder.x)
{
movement.x += fSpeed * Time.fixedDeltaTime;
}
}
}