- Home /
Freezing a RigidBody2D causes it to "accelerate"
Hi, I'm freezing a GameObject that has a RigidBody2D manaully (I'm not using RigidBodyConstraints2D). It stops moving alright, but while I was debugging other things I noticed that if I read it's velocity (while it's frozen in space), it's actually accelerating! Any ideas on what could be causing this?
What do you mean by freezing it manually? Are forces still being applied to the object while it is frozen?
Answer by Bunny83 · Jun 20, 2015 at 12:12 AM
You said you're "freezing it manually". Do you mean you "teleported" it back to it's last position by setting it's position manually?
When you do something like that when a collision happens the rigidbody get a collision force applied (the collision response). If you force the object to the same position the force is applied as long as you're colliding. The force is ment to seperate the two colliding objects. Since you don't allow this the force builds up speed over time.
If you manually clamp the position in one or two axes you should also zero the velocity in the same axes.
I freeze it by recording the point of collision using Collision2D.contacts[0].point, then in update I manually set the position each frame. I can post my script if you'd rather see that.
Edit 1: I should mention that the x velocity stays constant (not 0) but the y velocity accelerates.
Edit 2: Here it is just in case you did want to see it.
using UnityEngine;
using System.Collections;
public class StickWhenHit : $$anonymous$$onoBehaviour {
private Vector3 stickPosition;
private bool stick = false;
private Transform tp;
void Start()
{
}
void Update()
{
if(stick && tp != null)
{
tp.position = stickPosition;
}
else if(tp == null)
{
stick = false;
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.name == "teleporter(Clone)" && stick == false)
{
tp = coll.gameObject.transform;
//tp.GetComponent<Rigidbody2D>().fixedAngle = true;
float hitXPos = coll.contacts[0].point.x;
float xPos = 0;
if(hitXPos > transform.position.x)
{
xPos = transform.position.x + GetComponent<BoxCollider2D>().bounds.extents.x + coll.gameObject.GetComponent<BoxCollider2D>().bounds.extents.x;
}
else if(hitXPos < transform.position.x)
{
xPos = transform.position.x - GetComponent<BoxCollider2D>().bounds.extents.x - coll.gameObject.GetComponent<BoxCollider2D>().bounds.extents.x;
}
stickPosition = new Vector3(xPos,coll.contacts[0].point.y,tp.position.z);
stick = true;
}
}
}
Well, regardless of possible collisions you probably have gravity still enabled. So you have a constant acceleration of 9.81 on the negative y axis. Usually this speed get resetted once you hit the floor.
Do you actually want any forces to affect your rigidbody? if not, why don't you simply set is$$anonymous$$inematic to true?
Yea that way is so much simpler and that's what I had ended up doing, I was just perplexed on what the specific issue was. If I understand it correctly, I was manually freezing it by setting it's position each frame, but the body was still subject to forces, like gravity. So it's velocity was increasing but it was unable to move because I was setting it's position.
Your answer
Follow this Question
Related Questions
Stuttering/jerky movement of kinematic rigidbody 2d moving by velocity in 2D game on iOS 0 Answers
Displaying the y value score,Displaying the score of the Y value 1 Answer
Make object stay inside circle 1 Answer
Jumping with Rigidbody.AddForce not working? 1 Answer
RigidBody velocity ends with weird floating value, when axis key is released. 3 Answers