- Home /
Stopped Rigidbody2D still applying motion to carried objects?
I have a strange condition going on with Rigidbody2D. I've got a mostly-working script that correctly applies movement to a platform.
However, the platform is clamped to keep it from moving beyond a certain point. When objects are carried with the platform, if the platform stops moving... the objects do not.
Forces should no longer be exerted on the platform at all, but things on top of it act as if they are, as in this example:
https://i.gyazo.com/8238a73f860214d8085ec81aca30f5ee.mp4
This is the code I have thus far:
using UnityEngine;
public class Player : MonoBehaviour
{
public bool isReady = false;
public float m_moveSpeed = 0;
public Rigidbody2D pRB;
public Transform m_Transform;
private Vector3 m_Velocity = Vector3.zero;
[Range(0, .3f)]
[SerializeField]
private float m_Smoothing = .05f;
// Start is called before the first frame update
void Start()
{
m_Transform = transform;
pRB = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
if (isReady) {
Vector2 targetVelocity = new Vector2(Input.GetAxis("Horizontal"), 0);
targetVelocity = m_Transform.TransformDirection(targetVelocity);
targetVelocity *= m_moveSpeed * Time.deltaTime;
Vector3 pos = transform.position;
if (pos.x < 4.5f || pos.x > -4.5f) {
pRB.velocity = Vector3.SmoothDamp(pRB.velocity, targetVelocity, ref m_Velocity, m_Smoothing);
}
}
}
private void LateUpdate()
{
Vector3 pos = m_Transform.position;
pos.x = Mathf.Clamp(pos.x, -5f, 5f);
m_Transform.position = pos;
}
}
All movement related to the platform should stop when it hits the clamp, however even with an additional if loop applied, it still acts as though there controls in effect. What am I doing wrong?
[1]: https://i.gyazo.com/8238a73f860214d8085ec81aca30f5ee.mp4