Home > Blockchain >  Why does Unity 2020 give an error CS1503?
Why does Unity 2020 give an error CS1503?

Time:03-02

I am new to Unity and C# code. I am doing a basic step by step tutorial from Brackeys and for no obvious reason I get this error that says "error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector3'". The error appears on both rb.AddForce lines. Does anyone know what's the problem here? I am using Unity 2020.3.29f1 Personal.

Thanks for your help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public Rigidbody rb;

    public float forwardForce = 2000f;

    void FixedUpdate()
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("d") )
        {
            rb.AddForce(500 * Time.deltaTime);
        }

        if (Input.GetKey("a") )
        {
            rb.AddForce(-500 * Time.deltaTime);
        }
    }
}

CodePudding user response:

There are different overloads of AddForce.

AddForce(Vector3 [, ForceMode])

and

AddForce(float, float, float [, ForceMode])

where the ForceMode is optional for both.

You are passing in only one single parameter float so the compiler thinks you want to use the first overload and tries in vain to convert the given float into a Vector3 and obviously doesn't find any implemented way to do such thing.

You probably rather want

rb.AddForce(Vector3.right * 500 * Time.deltaTime);

or

rb.AddForce(500 * Time.deltaTime, 0, 0);
  • Related