- Home /
2D game - Sprite deformation when add frce
Hi.
I created an image and added to it Collider, Rigidbody2D. Also I create script with function which called when pressing the space key and AddForce to this image. After when I start project and press Space key I saw that object start deformed. Whel it collision with other object Player very deformaed. It is deformed and stretched in width upwards. But when I set equal force for X and Y axis Player didn't deformed. For example
transform.AddForce(500f, 3000f); // Deformed
transform.AddForce(3000f, 3000f);// Not deformed
Ball components
Ball script with AddForce
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Add force");
GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce));
}
}
What do to fix this problem?
No idea what this means and your images don't help either.I see, that sprite start deformation.
No idea what the prblem is beyond just guessing. You should spend more time trying to explain the problem in detail rather than sum it up with things like 'sprite deformation' if you want other people to spend time trying to help you.What do to fix this problem?
Ok. I updated question with more descriptions of problem.
Also transform.AddForce(float x, float y) is not an existing transform function. RigidBody2D has a function AddForce(Vector2 force). That code is very weird.
Exactly what I was going to say! @stas0 - please post the actual code you're using, because what you've written will just generate a compiler error. Also please post a screenshot of what this "deformation" looks like.
In fact he posted it :
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
Debug.Log("Add force");
GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce));
}
}
but thats really bad code with the get of RigidBody each time ins$$anonymous$$d of caching it in Start!
Your ball also seems to have a different scale in XY thats maybe why you are getting different scale -_- cause like @$$anonymous$$elv$$anonymous$$ay saidm it's impossible for a rigidbody to scale a transform. Try cleaning your code and have lower RigidBody values and put Gravity Scale to 1.
I see you changed the code but use a Gravity scale of 1, which represents 9.8 m/^s like on Earth. Also be sure nothing else affect the scale.
Can you try to reduce also the force applied, like only 100 ?
If that doesn't work, there is something wrong that affect the size of your object...
I test with 100 value and it is didn't help. Also I test with other object the smae code and all work fine. I will search a problem
Answer by MelvMay · Nov 13, 2016 at 03:38 PM
Can you please explain what you mean by 'deformed'? Do you mean the scale changes? Adding a force or colliders contacting do not change the Transform scale so you must have some other script/animation doing that.
Adding a force only changes the velocity of a Rigidbody2D and nothing else.
Sorry for my bad english, 'deformed' I mean the deformation. I don't have any script that do deformation with ball, only ball script that call AddFroce when press Space key. After this ball start change scale.
I honestly have no idea. There's no way a Rigidbody2D can set the scale of the Transform. The only other option is to submit a bug case for Unity QA to look at it.
Answer by amimox · Nov 13, 2016 at 04:22 PM
Hi ! First of all you shouldn't use GetComponent method each frame just to get the same rigidbody. This is very bad for performance.
private RigidBody2D rb;
void Start(){
rb = GetComponent<RigidBody2D>();
}
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Add force");
rb.AddForce(new Vector2(xForce, yForce));
}
}
Also don't use a Gravity scale of 110. That is very bad and can cause severe problem. This show a problem in your current hierarchy. Try to tweak the size of your game object instead of computing crazy Physics parameters.