Position and collider seem to incorrectly shift when moving object
Hey guys, I'm pretty new to Unity and I'm currently trying to implement small algorithms to move objects around just for fun.
I've read a little about how and when to use the Update or the FixedUpdate loop, or to move a gameobject's rigidbody with MovePosition() instead of using Transform.Translate().
I'm currently trying to create some moving platforms to interact with my player, but I've been bumping into a weird problem recently. I cannot exactly pinpoint its exact source, but it seems that, when I add speed to the platform, its position seems to shift in the opposite direction in the editor while moving.
For reference, here's a top down view of my moving platform heading to the right with a speed of 1, where I've drawn a line from its center to its top right back corner.
Here's the same platform, but with a speed of 15.
As you can see, the theoretical position of my platform seems to move as the speed increases, which causes problems with collision detection directly in the editor and OnCollisionEnter/Stay/Exit methods. At that speed, because of the shift, if I place a gameobject a couple of pixels to the left of the platform, a collision occurs and the object follows the platform's movements, even if the two are not visually colliding. The opposite seems to happen on the other side of the platform, where a collision should occur, but does not.
I've noticed that if I replace the FixedUpdate loop by an Update loop, the line seems to be correctly aligned, no matter the speed, but I've read that any physics related operation should preferably be executed in the FixedUpdate loop. Even so, switching to an Update loop does not fix the collision detection problem.
The platform is a simple gameobject with a kinematic rigidbody (where I froze the rotation on each axis) and a box collider wrapping the whole object.
Here's the code I'm using for my moving platform.
public class MovingPlatform : MonoBehaviour
{
[SerializeField]
private List<Vector3> _positions;
[SerializeField]
private float _speed;
private int index;
private bool reverse;
private Rigidbody _rigidbody;
private Dictionary<GameObject, Rigidbody> _attachedGameObjects;
public void Awake()
{
_attachedGameObjects = new Dictionary<GameObject, Rigidbody>();
_rigidbody = gameObject.GetComponent<Rigidbody>();
}
public void Start()
{
_positions.Insert(0, gameObject.transform.position);
}
public void FixedUpdate()
{
if (gameObject.transform.position == _positions[index])
{
ChooseNextReachPoint();
}
float step = _speed * Time.fixedDeltaTime;
Vector3 newPosition = Vector3.MoveTowards(gameObject.transform.position, _positions[index], step);
Vector3 moveValue = newPosition - gameObject.transform.position;
foreach (KeyValuePair<GameObject, Rigidbody> attachedGameObject in _attachedGameObjects)
{
Vector3 newAttachedGameObjectPosition = attachedGameObject.Key.transform.position + moveValue;
attachedGameObject.Value.MovePosition(newAttachedGameObjectPosition);
}
_rigidbody.MovePosition(newPosition);
Debug.DrawLine(gameObject.transform.position, gameObject.transform.position + transform.localScale / 2,
Color.red, Time.fixedDeltaTime, false);
}
private void ChooseNextReachPoint()
{
if (index == 0)
{
reverse = false;
}
else if (index + 1 == _positions.Count)
{
reverse = true;
}
index += reverse ? -1 : 1;
}
private void OnCollisionStay(Collision collision)
{
//TODO: Only add if on top of platform
if (!_attachedGameObjects.ContainsKey(collision.gameObject))
{
_attachedGameObjects.Add(collision.gameObject, collision.rigidbody);
}
}
private void OnCollisionExit(Collision collision)
{
_attachedGameObjects.Remove(collision.gameObject);
}
}
Is there anything wrong with my understanding of the FixedUpdate vs Update concept, my code, or anything else?
Thanks!
Your answer
Follow this Question
Related Questions
Issue with AI falling through the map using a Character Controller 0 Answers
Rigidbody is colliding terrain on isKinematic = false. call although meshes don't touch 0 Answers
Is there a way to detect particle collision on rigidbody trigger collider? 0 Answers
Kinematic forces under Rigidbody Parents 0 Answers