Search Unity

error CS1503: Argument 1: cannot convert from 'int' to 'UnityEngine.Vector2'

Discussion in '2D' started by yazanSYR, May 27, 2020.

  1. yazanSYR

    yazanSYR

    Joined:
    May 27, 2020
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public Rigidbody2D rb;

    // Update is called once per frame
    void FixedUpdate()
    {
    rb.AddForce(1000, 0);
    }
    }


    So I just started unity a day ago and I want to make a game that has a character (block) automatically moving on a platform and the player has to dodge obstacles and such (such an original idea right?), but so far I just got the platform ready and the player who's supposed to move, I'm still on the first step which is figuring out who to make the player move automatically to the right, the component is being added to "Player" and I keep getting that error on the title of this thread. I would appreciate it if someone could just send me the full code that I should write and I would appreciate it much much more if I were to be told what's exactly wrong so I don't make any future mistakes similar to this one (2D game btw).
    Everything Is indented correctly btw i don't know why my post on preview is shown without indentation
     
  2. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    Code (CSharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.         public Rigidbody2D rb;
    5.  
    6.     // Update is called once per frame
    7.     void FixedUpdate()
    8.     {
    9.             vector3 vf = new vector3(1000,0,0);
    10.             rb.AddForce(vf);
    11.     }
    12. }
    add force takes a vector because it needs direction, in this case a vector is a (x,y,z) direction and you are specifying 1000 units of force toward the right.

    it would be cleaner like this:
    Code (CSharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.         public Rigidbody2D rb;
    5.  
    6.     // Update is called once per frame
    7.     void FixedUpdate()
    8.     {
    9.             float force = 10f;
    10.             rb.AddForce( vector3.right * force);
    11.     }
    12. }
    this assumes your direction is going right and multiplies it by the amount of force you want to apply
     
    yazanSYR likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    When posting code it doesn't automatically recognise code. Here's how you use Code Tags.

    So to be clear, this isn't a "Unity" thing, it's a C# language thing. You called Rigidbody2D.AddForce() but if you look at the docs, it doesn't take an int (you passed "1000") but instead takes a Vector2. The Vector2 is a direction and magnitude of the force you want to apply.

    EDIT: I noticed you got a reply just before mine. Note that this is mostly correct but you should use Vector2 instead of "vector3" (note that it is case sensitive too). Unity will convert Vector3 to Vector2 by dropping the Z but this can be the source of subtle issues sometimes so stick to Vector2 where it makes sense.
     
    brigas and yazanSYR like this.