[SOLVED] I'm having trouble with Vector2 in rigidbody2d.addForce(vector2,type)

Trying to fix a (yes temporary) problem with the Ball in block breaker, specifically trying to apply a constant lateral force to prevent ball from getting stuck in an endless up/down loop. ( I realize this is no problem once blocks break)

For some reason I have no problem applying a torque but a force is proving difficult. I’m sure I am missing something but no matter where I put the variable declaration, I seem to get an error trying to specify a Vector2 x,y force. It is acting like I am not using Vector2 correctly.

the Vector2 variable addedForce is the forece I want to add ( eg. 0.2,0.0)
using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
public Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStarted= false;// has the game started?
private Vector2 addedForce;

void Start () {
	paddleToBallVector = this.transform.position - paddle.transform.position; //the offset to paddle
	print (paddleToBallVector.y);

13 addedForce = (0.02,0.0); // how much sideways force to add to prevent stalling loop of ball

}


void Update () {;
	if (!hasStarted){// do while game not started
		this.transform.position = paddle.transform.position + paddleToBallVector;//locks ball relative to paddle
		if (Input.GetMouseButtonDown (0)){
			print ("left mouse button clicked so Launch Ball!");
			this.rigidbody2D.velocity = new Vector2 (2f, 10f);//shoot ball up
			this.rigidbody2D.AddForce(addedForce, ForceMode2D.Force);
			this.rigidbody2D.AddTorque(0.6f,ForceMode2D.Impulse);
			hasStarted = true;//game has started
		}
	}

}

}

error message is:
Assets/Scripts/Ball.cs(13,35): error CS1026: Unexpected symbol ,', expecting)’

I’m sorry this post came out so messy

Hey Walshig,

Your addedForce is what Type? Vector2 or something else?

addedForce = (0.02,0.0); <—You missed the “f”

Vector2 addedForce = new Vector2 (0.02f, 0.0f);

I noticed you have a semicolon right after the first open curly brace.

void Update () {;  <--HERE********************
	if (!hasStarted){// do while game not started
		this.transform.position = paddle.transform.position + paddleToBallVector;//locks ball relative to paddle
		if (Input.GetMouseButtonDown (0)){
			print ("left mouse button clicked so Launch Ball!");
			this.rigidbody2D.velocity = new Vector2 (2f, 10f);//shoot ball up
			this.rigidbody2D.AddForce(addedForce, ForceMode2D.Force);
			this.rigidbody2D.AddTorque(0.6f,ForceMode2D.Impulse);
			hasStarted = true;//game has started
		}
	}

}

Also for you to user add force, you need a rigidbody2d on the gameobject, not sure if you have it, but just a thought.

Everything else looks fine to me.

Thanks for pointing out the extra ; but the problem remains with trying to stick values in the vector2 variable, with or without the ‘f’
here is a screenshot, keeps line numbers better:

For your Vector2 addedForce, you may have to assign it a new Vector2 (0.02f, 0.0f)

Hence:

void Start(){
       addedForce = new Vector2 (0.02f, 0.0f);
}

yes, missed the ‘new’ thanks a bunch,
best of all, it works and ball wont stall any more!

I don’t really understand when we need new and when we don’t

Awesome!

Maybe this helps to understand?

1 Like

that is a good thread, there is a great example showing what goes on:

Privacy & Terms