using System;
using JetBrains.Annotations;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class Movement : MonoBehaviour
{
Rigidbody2D rb; //ignore the unnecessary amount of stupid variables. I'll fix it.
public float baseTargetVelocityVert;
public float baseTargetVelocityHoriz;
public float velocityVert;
public float velocityHoriz;
float targetVelocityVert;
float targetVelocityHoriz;
float forceToApplyVertMult;
float forceToApplyHorizMult;
float percentTargetVelReachedVert;
float percentTargetVelReachedHoriz;
public float brakeStrengthVert;
public float brakeStrengthHoriz;
float brakeForceVert;
float brakeForceHoriz;
float pushForceVert;
float pushForceHoriz;
float isGoingNegativeVertMult;
float isGoingNegativeHorizMult;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
baseTargetVelocityHoriz = 5f;
baseTargetVelocityVert = 3f;
brakeForceHoriz = 1f;
brakeForceVert = 1f;
pushForceHoriz = 1;
pushForceVert = 1;
isGoingNegativeHorizMult = 0;
isGoingNegativeVertMult = 0;
}
// Update is called once per frame
void Update()
{
//nothing here yet.
}
private void FixedUpdate()
{
velocityHoriz = (Vector3.Dot(rb.linearVelocity, transform.right));
velocityVert = (Vector3.Dot(rb.linearVelocity, transform.up));
if (rb != null)
{
targetVelocityVert = 0f;
if (Keyboard.current.wKey.isPressed)
{
targetVelocityVert += 1f;
Debug.Log("w is pressed");
}
if (Keyboard.current.sKey.isPressed)
{
targetVelocityVert -= 1f;
Debug.Log("s is pressed");
}
targetVelocityVert *= baseTargetVelocityVert; //this is the "speed i want to reach that is also the max speed" calculation for vertical movement.
targetVelocityHoriz = 0f;
if(Keyboard.current.dKey.isPressed)
{
targetVelocityHoriz -= 1f;
Debug.Log("d is pressed");
}
if(Keyboard.current.aKey.isPressed)
{
targetVelocityHoriz += 1f;
Debug.Log("a is pressed");
}
targetVelocityHoriz *= baseTargetVelocityHoriz; //this is the "speed i want to reach that is also the max speed" calculation for horizontal movement.
if(targetVelocityVert != 0f)
{
if(Mathf.Sign(targetVelocityVert) < 0)
{
isGoingNegativeVertMult -= 1f;
}
else
{
isGoingNegativeVertMult += 1f;
}
if (Mathf.Abs(velocityVert) < Mathf.Abs(targetVelocityVert)) //if not at max vertical speed, get there.
{
percentTargetVelReachedVert = (Mathf.Abs(velocityVert * 100)) / Mathf.Abs(targetVelocityVert);
forceToApplyVertMult = (1f - percentTargetVelReachedVert);
}
else
{
forceToApplyVertMult = 0f;
}
Debug.Log("adding force.");
rb.AddForce(transform.up * (pushForceVert * forceToApplyVertMult * isGoingNegativeVertMult));
}
else //if the speed we want to get to is 0 (because nothing is pressed) brake.
{
if(Mathf.Abs(velocityVert) < 0.5f)
{
rb.linearVelocity -= (Vector2)(transform.up * velocityVert);
}
else
{
brakeForceVert = brakeStrengthVert * Mathf.Sign(velocityVert) * -1f;
rb.AddForce(transform.up * (brakeForceVert));
}
}
if(targetVelocityHoriz != 0f)
{
if (Mathf.Sign(targetVelocityVert) < 0)
{
isGoingNegativeHorizMult -= 1f;
}
else
{
isGoingNegativeHorizMult += 1f;
}
if (Mathf.Abs(velocityHoriz) < Mathf.Abs(targetVelocityHoriz)) //if not at max horizontal speed, get there.
{
percentTargetVelReachedHoriz = (Mathf.Abs(velocityHoriz * 100f)) / Mathf.Abs(targetVelocityHoriz); //1
forceToApplyHorizMult = (1f - percentTargetVelReachedHoriz);
}
else
{
forceToApplyHorizMult = 0f;
}
rb.AddForce(transform.right * (pushForceHoriz * forceToApplyHorizMult * isGoingNegativeHorizMult));
}
else //if the speed we want to get to is 0 (because nothing is pressed) brake.
{
if (Mathf.Abs(velocityHoriz) < 0.5f)
{
rb.linearVelocity -= (Vector2)(transform.right * velocityHoriz);
}
else
{
brakeForceHoriz = brakeStrengthHoriz * Mathf.Sign(velocityHoriz) * -1f;
rb.AddForce(transform.right * (brakeForceHoriz));
}
}
}
}
}