Jump to content
  • Advertisement
ethancodes

C# objects not moving as directed until they fall and hit something

Recommended Posts

I've got a bunch of balls all spawning in a timed interval and I am setting the velocity of them when they spawn so that they will spawn and immediately start going in the direction I want. However, they don't do that. Instead they spawn in place and gravity makes them begin to fall straight down. if they hit something, then they take off like just like they should have spawned. I'm not sure why they have to hit something for this to happen. Any ideas? The code below is the ball spawning code. The mainballVector is set in a different section and I have verified it does have the correct values. 

 

		Instantiate(newBall, new Vector3(paddle.transform.position.x, (paddle.transform.position.y + 1), 0), Quaternion.identity);
		newBall.GetComponent<Ball>().hasStarted = true;
		GameManager.pickUpManager.allBalls.Add(newBall.GetComponent<Ball>());
		newBall.GetComponent<Rigidbody2D>().velocity = mainBallVector; 		//use the original ball's vector3 to follow it directly

 

Share this post


Link to post
Share on other sites
Advertisement

Try calling "AddForce" rather than setting the velocity by hand... 

something like this:

newBall.GetComponent<Rigidbody2D>().AddForce(mainBallVector); 	

 

Share this post


Link to post
Share on other sites
23 hours ago, ethancodes said:

spawn in place and gravity makes them begin to fall straight down.

You should remove gravity from the object you are using for Instantiate. The "NewBall" object. Unless you want gravity?

Share this post


Link to post
Share on other sites
18 minutes ago, ethancodes said:

Tried this, but I'm getting the same result.

Have you tried with a ForceMode2D parameter too? (Like, right after the force)

If so, then maybe there's somewhere else during the frame that just removes your previously set velocity...

I do kind of the same thing (except in 3D). I instantiate rigidbodies and add a force to them...

Share this post


Link to post
Share on other sites
10 minutes ago, Scouting Ninja said:

You should remove gravity from the object you are using for Instantiate. The "NewBall" object. Unless you want gravity?

Yes i do need gravity.

 

10 minutes ago, miimii1205 said:

Have you tried with a ForceMode2D parameter too? (Like, right after the force)

If so, then maybe there's somewhere else during the frame that just removes your previously set velocity...

I do kind of the same thing (except in 3D). I instantiate rigidbodies and add a force to them...

I have not tried that... I'll give it a shot real quick.

Ok that did not work, but let me backtrack a little bit. I could have sworn when I checked before I was getting the vector of "mainBall", but maybe I was mistaken cause I just checked it again and it's all 0's. This is the code for grabbing the vector from mainBall:

mainBallVector = mainBall.GetComponent<Rigidbody2D>().velocity;

Am I missing something here as to why it would not be grabbing the info from this?

Share this post


Link to post
Share on other sites

Scratch that, it is in fact getting the vector from the original ball, but for whatever reason, it is not launching the ball when it is handed the vector. I'm really not sure why this would be. Everything looks right to me.

Share this post


Link to post
Share on other sites

@ethancodes does the original ball get its velocity changed?

If not, maybe you could try debugging it with breakpoints.

If you don't know how, Unity has a tutorial on this matter.

For example, you could just put breakpoints before and after the velocity change and see if your balls' velocity changes at all.

Share this post


Link to post
Share on other sites

I got it! Someone on stackoverflow pointed out that I was not actually accessing the instance of the newBall, but rather the prefab. So I put var ball = Instantiate blah blah blah and that fixed it. Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!