- Home /
The question is answered, right answer was accepted
2D physics slow moving down
In my game I have a ball that moves around the screen, I am using this script to make it work, it has a 2DRigidbody attached. For some reason when I press down to move down, it moves down slowly as compared to everyother direction. Why is this happening?
if(this.name == "redBall")
{
bool movingX = (Input.GetAxis("Horizontal") != 0);
bool movingY = (Input.GetAxis("Vertical") != 0);
float _xAccel = 0, _yAccel = 0;
_xAccel = Input.GetAxis("Horizontal");
_yAccel = Input.GetAxis("Vertical");
Vector2 vec = new Vector2(_xAccel, _yAccel);
GetComponent<Rigidbody2D>().AddForce(vec * Speed);
GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxSpeed);
float newX = GetComponent<Rigidbody2D>().velocity.x, newY = GetComponent<Rigidbody2D>().velocity.y;
if (!movingX && newX > 0)
newX -= slipperiness;
else if (!movingX && newX < 0)
newX += slipperiness;
if (!movingY && newY > 0)
newY -= slipperiness;
else if (!movingX && newY < 0)
newY += slipperiness;
if (newX > 0 && newX < slipperiness)
newX = 0;
if (newX < 0 && newX > (-slipperiness))
newX = 0;
if (newY > 0 && newY < slipperiness)
newY = 0;
if (newY < 0 && newY > (-slipperiness))
newY = 0;
GetComponent<Rigidbody2D>().velocity = new Vector2(newX, newY);
}
EDIT : I just figured out that if I use AddForce() I get this problem, but when I directly change the velocity it moves the same speed in all directions, but with changing it's velocity it doesn't react the same with other colliders in the scene the way it did with AddForce(). It does not bounce as much it kinda bounces a centimeter than sticks to it. It also speeds up very quickly to maxSpeed and doesn't need the part of the script that slows it down.
Vector2 vec = new Vector2(_xAccel, _yAccel);
//GetComponent<Rigidbody2D>().AddForce(vec * Speed);
GetComponent<Rigidbody2D>().velocity = new Vector2(vec.x * Speed, vec.y * Speed);
GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxSpeed);
float newX = GetComponent<Rigidbody2D>().velocity.x, newY = GetComponent<Rigidbody2D>().velocity.y;
else if (!movingX && newY < 0) newY += slipperiness;
Should be movingY
Follow this Question
Related Questions
Is there a standardized way for creating a 2D ball physics movement method? 0 Answers
Physics2D Overlapbox not working as aspected 0 Answers
Maintain equal physics over different UI sizes? 2 Answers
Game not working on Tizen 0 Answers
Should a physics material be added to the collider or the rigidbody? 2 Answers