Hello everyone.
I have spent most of the day trying to simulate an increase in gravity on a ball in my little 2D breakout game. Its basic stuff that I want to do so I wish to check the right way to go about it.
I tried to increase the Gravity Scale on the Balls Rigid Body at runtime but the ball is ignoring the value. If I stop the game and run it again I see the new value reflected in the Inspector and it gets applied but only if I stop and start the game. Is there anyway to actually invoke the change at runtime?
Here is the code for my my Ball.
public void IncreaseBallGravity()
{
float ballGrav = GetComponent<Rigidbody2D>().gravityScale; //load ballGrav with the current ball gravity
ballGrav++; //increment ballGrav
print("ballGrav = " + ballGrav);
GetComponent<Rigidbody2D>().gravityScale = ballGrav;
}
I have also looked at Physics.gravity in the Unity docs but I am unsure if this is what I want.
I have tried to use Physics.gravity like so
GetComponent<Physics2D>().gravity
but intellisense does not like.gravity so I know I am doing it wrong. I have tried looking online and I hear mention that Physics.gravity being a global thing so I came to ask what I should use to achieve this.
I wish to be able to change the gravity of a specific object at runtime and get the same effect as changing as I do when I increase the RigidBody2D Gravity Scale before starting the game.
Thanks for any help you can give.