Search Unity

Argument 1: cannot convert from "float" to "UnityEngine.Vector3"

Discussion in 'Physics' started by TrineHat, May 12, 2019.

  1. TrineHat

    TrineHat

    Joined:
    May 12, 2019
    Posts:
    2
    I'm trying to learn how to make something move and I'm following a video for it. I did exactly what they did and I've asked someone who knows how to code and apparently it all seems correct. Hopefully someone can tell me what's wrong. This is all the code I've got. The error appears over "(-sidewaysForce)". Thank you.

    using UnityEngine;


    public class Player1Movement : MonoBehaviour
    {

    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // Update is called once per frame
    void FixedUpdate ()
    {
    rb.AddForce(0, 0, forwardForce * Time.deltaTime);

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

    if (Input.GetKey("a"))

    { rb.AddForce(-sidewaysForce) * Time.deltaTime, 0, 0);
    }
    }
    }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,444
    First, there is a little button when you are making a new forum posting, to include formatting for your code. It helps when others have to read code.

    Second,
    sidewaysForce
    is just a number, not a direction. Forces have direction, so the
    rb.AddForce()
    method expects a vector, or three numbers. If you put three things in the parentheses, then they need to be three floats. If you put one thing in the parentheses, it needs to be a Vector3. You have an extra
    )
    so it only sees one argument.

    Many people would also use
    (Vector3.right * sidewaysForce * Time.deltaTime)
    in those cases to send a vector.
     
    TrineHat likes this.
  3. TrineHat

    TrineHat

    Joined:
    May 12, 2019
    Posts:
    2

    Ironically... I came across this problem after staying up till 7am. I just figured it out and I don't understand WHY the error was so complicated when it was just a misplaced bracket but your answer helps. I'll keep what you said in mind for future posts. Thank you so much!