0
votes

I'm new to Unity and to C#. I was trying to code a 2D platformer movement script, but for some reasons the code I'm creating doesn't work.

The script is referred to a circle. I've added "Rigidbody2D" and "Circle Collider 2D".

I've tried to use this script:

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

public class Movement : MonoBehaviour

{
    public Rigidbody2D rb;

    public void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rb.AddForce(10, 0, 0);
        }
    }
}

The code should give an hit to the circle to make it move right, but Visual Studio says that "rb.AddForce" is an error. Can you help me, please?

3
Can you be more specific - what error does Visual Studio say is happening? - Vlad274
@Vlad274 On the Unity debug console it says "error CS1501: No overload for method 'AddForce' takes 3 arguments" - AlexxS_

3 Answers

4
votes

Are you sure you have actually referenced the rigidbody? Did you drag the rigidbody in the editor? If you have not, you could also say the following (if the script is attached to object that holds the rigidbody you would like to move):

private Rigidbody2D rb;

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

1)Make sure your Rigidbody component is NOT set to Kinematic.

2)Depending on the mass and linear drag of the rigidbody, you would need to change the force you apply to it accordingly. The code may be working but you would not see the body moving if you do not apply enough force.

3)Addforce() expects a Vector as an argument. This is your problem.

public float thrust; //set in editor, this is how strong you will be pushing the object 

private Rigidbody2D rb;


private void Start()
{
  rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
    if (Input.GetKey(KeyCode.RightArrow))
    {
        rb.AddForce(transform.right * thrust); //this will move your RB to the right while you hold the right arrow
    }
}

4) Set a linear drag of your rigidbody so that it can actually stop after applying the force to it. In order to make it work, set the mass and linear drag both to 1 for example and then just experiment with the thrust variable, it will eventually start moving. After that you can reduce/increase the linear drag and the thrust until you achieve the desired effect.

BONUS If you wish to use a Vector3D the way you have tried in your code, you could do the following and it will work too:

private void FixedUpdate()
{
    if (Input.GetKey(KeyCode.RightArrow))
    {
        rb.AddForce(new Vector3(10, 0, 0)); //this will move your RB to the right while you hold the right arrow
    }
}
0
votes

Because of Rigidbody2D implementation it takes Vector2 in constructor as an argument instead of simple Rigidbody which can take Vector3 and Vector2 as Vector3. Consider Vector3 v3 = new Vector2(10, 0); and Vector2 v2 = new Vector3(10, 0, 0);

Try this

rb.AddForce(new Vector2(10, 0));

or

rb.AddForce(new Vector3(10, 0, 0));
0
votes

You need to add ForceMode2D.Impulse for it to work:

using UnityEngine;

public class TestControl : MonoBehaviour
{
    public Rigidbody2D rb2d;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            rb2d.AddForce(new Vector2(5, 0 ), ForceMode2D.Impulse);
            Debug.Log("RightArrow was Pressed");
        }

    }
}

You can find more info in here: https://www.studytonight.com/game-development-in-2D/right-way-to-move