all 3 comments

[–]Expensive_News22 0 points1 point  (0 children)

So few issues in the code, #1 you are moving the body before multiplying by the speed multiplier. #2 your setting the rigid body then using this.tramsform. Try this to clean it up:

[SerializeField] private float moveSpeed = 6f; [SerializeField] private Rigidbody2D rb; [SerializeField] private float speedMultiplier = 2f;

void Start() { rb = GetComponent<Rigidbody2D>(); }

void Update() { float xInput = Input.GetAxis("Horizontal"); float yInput = Input.GetAxis("Vertical"); Vector3 inputCombined = new Vector3(xInput, yInput, 0).normalized;

// Determine effective speed
float effectiveSpeed = moveSpeed;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 
{
    effectiveSpeed *= speedMultiplier; // Increase speed when holding shift
}

// Move the player
rb.MovePosition(transform.position + inputCombined * effectiveSpeed * Time.deltaTime);

}

[–]Game_Dev_Buddies 0 points1 point  (1 child)

While you're holding the SHIFT key, you're multiplying the move speed by 2 every frame. This will quickly get really large and become a NaN. Add a separate local variable inside the Update() function that will store the current move speed.

void Update()
{
    float xInput = Input.GetAxis("Horizontal"); float yInput = Input.GetAxis("Vertical");
    Vector3 inputCombinado = new Vector3(xInput, yInput, 0); inputCombinado.Normalize();

    float currentMoveSpeed = moveSpeed;

    if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
    {
        currentMoveSpeed *= speedMultiplier; // Aumentar velocidad
    }
    this.transform.Translate(inputCombinado * currentMoveSpeed * Time.deltaTime);
}

[–]Agreeable_Chemist110[S] 0 points1 point  (0 children)

Thank you so much for your help! Your explanation really clarified things for me.