Error fix needed

public class Movement : MonoBehaviour
{
    private Vector3 mousePosition;
    public float moveSpeed = 0.5f;
    public bool isMoving = false;
   
    public float dashEndSpeed;
    public float lastDash;
    public float dashCooldown = 1f;
    public float dashTime = 0.2f;
    public bool isDashing = false;
    public float dashSpeed;
    void Update()
    {
        if (Input.GetMouseButtonDown(1) && isMoving == false)
        {
            isMoving = true;
        }
        else if (Input.GetMouseButtonDown(1) && isMoving == true)
        {
            isMoving = false;
        }
        if (Input.GetMouseButtonDown(0) && isMoving == false && lastDash < dashCooldown)
        {
            lastDash = Time.time;
            isMoving = true;
            dashEndSpeed = moveSpeed;
            dashSpeed = dashEndSpeed * 5;
            moveSpeed = dashSpeed;
            isDashing = true;
            do
            {
                if (moveSpeed = dashSpeed && lastDash < dashTime) //error here
                {
                    isMoving = false;
                    moveSpeed = dashEndSpeed;
                    isDashing = false;
                }
            } while (moveSpeed = dashSpeed); //error here
        }
        else if (Input.GetMouseButtonDown(0) && isMoving == true && lastDash < dashCooldown)
        {
            lastDash = Time.time;
            dashEndSpeed = moveSpeed;
            dashSpeed = dashEndSpeed * 5;
            moveSpeed = dashSpeed;
            isDashing = true;
            do
            {
                if (moveSpeed = dashSpeed && lastDash < dashTime) //error here
                {
                    isMoving = false;
                    moveSpeed = dashEndSpeed;
                    isDashing = false;
                }
            } while (moveSpeed = dashSpeed); //error here
        }
    }
    void FixedUpdate()
    {
        if (isMoving == true)
        {
            mousePosition = Input.mousePosition;
            mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
            transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed * Time.deltaTime);
        }
    }
}

My errors here are where I have marked them with ‘//error here’, and they are as follows:

  • after each marked if statement I receive the message ‘Assets\Scripts\Player\Movement.cs(49,22): error CS0029: Cannot implicitly convert type ‘float’ to ‘bool’’

  • and after each while statement marked, I receive this ’
    Assets\Scripts\Player\Movement.cs(42,33): error CS0019: Operator ‘&&’ cannot be applied to operands of type ‘float’ and ‘bool’

I’ve been trying for a while to fix this and can’t figure it out, so I figured I’d ask here then go to bed, see what fixes I get in the morning. thanks :).

Equal sign (=) is assignment, and returns the value (a float).

You probably want == to compare, which returns a bool.

HOWEVER! Do not compare floating point values for equality, due to floating point error.

Instead, use Mathf.Approximately(), or do your own absolute epsilon delta check.

thanks for the fix, I might not use the second part though because since this code has been written to have scaling dash speed comparative to movement speed upon dash entry, and if the unity engine were only looking for movement speed being approximate to the dash speed and the player entered their dash with incredibly low movement speed, then the approximate may think that the player is still moving at dash speed when they’re not after the dash is finished. this wouldn’t be a problem except for the fact that it would constantly be setting the isMoving bool to whatever it was when the dash was initiated, preventing the player from stopping/starting movement.

Is there a fix for the second part?

do
            {
                if (moveSpeed = dashSpeed && lastDash < dashTime) //here
                {
                    isMoving = false;
                    moveSpeed = dashEndSpeed;
                    isDashing = false;
                }

            } while (moveSpeed == dashSpeed);

error being:
Assets\Scripts\Player\Movement.cs(61,33): error CS0019: Operator ‘&&’ cannot be applied to operands of type ‘float’ and ‘bool’

Um… it’s the same fix. :slight_smile:

Oh shit I’m dumb XD, completely didn’t see that because the error focused on the &&