# 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;
newBall.GetComponent<Rigidbody2D>().velocity = mainBallVector; 		//use the original ball's vector3 to follow it directly

##### Share on other sites

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

something like this:

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

##### Share on other sites

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

##### 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 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 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 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 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 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!

## Create an account

Register a new account

1. 1
2. 2
3. 3
4. 4
frob
12
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632172
• Total Posts
3004563

×