dkub7
1
Hey guys, Im new to programming and dont know how addforce and vector2 works, may yall help me out?
My script is:
private Rigidbody2D rb;
public float Speed = 13f;
public float Jump = 12f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
rb.AddForce(Speed,0f,0f);
if (Input.GetMouseButtonDown(0))
{
rb.AddForce(0, 0, Jump);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Obstacle")
{
Destroy(gameObject);
}
}
thanks 
Try the following code:
public float speed = 13.0f;
public float jump = 12.0f;
private Rigidbody2D _rigidbody2D;
private void Start()
{
_rigidbody2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
_rigidbody2D.AddForce(new Vector3(speed, 0.0f, 0.0f));
if (Input.GetMouseButtonDown(0))
{
_rigidbody2D.AddForce(new Vector3(0.0f, 0.0f, jump));
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Obstacle"))
{
Destroy(gameObject);
}
}
And please, before posting questions be sure to check out the documentation: (Unity - Scripting API: Rigidbody2D.AddForce)