objects not moving as directed until they fall and hit something

Started by
8 comments, last by ethancodes 5 years, 8 months ago

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

 

Advertisement

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

something like this:


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

 

Trying to change the worldMeanwhile, go here: Bandcamp | YouTube

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

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?

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...

Trying to change the worldMeanwhile, go here: Bandcamp | YouTube
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?

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.

@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.

Trying to change the worldMeanwhile, go here: Bandcamp | YouTube

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!

This topic is closed to new replies.

Advertisement