Collision Not Working Properly

Hey Guys, I have this problem. When I move fast objects pass through the collisions. I think this is because I use transform positions. When I make it move slower, it’s okay. But it is boring that way. How can I solve this problem?

public class MoveToPoint : MonoBehaviour
{
    private Touch _touch;
    private float _dragSpeed = 0.005f;
    private float _cycleLength = 21f;
    private float _limitZ = 10f;
    [SerializeField] Rigidbody _rb;
    [SerializeField] GameObject plane;
    private float _stopPoint;

    void Start()
    {
        Debug.Log(plane.GetComponent<MeshRenderer>().bounds.max.x);
       _stopPoint = plane.GetComponent<MeshRenderer>().bounds.max.x;
        Movement();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        DragControl();
        
    }


    public void DragControl()
    {



        if (Input.touchCount > 0)
        {
            _touch = Input.GetTouch(0);

            if (_touch.phase == TouchPhase.Moved)
            {
                Vector3 pos = transform.position;
                transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + _touch.deltaPosition.x * (-_dragSpeed));
                //_rb.velocity = new Vector3(transform.position.x, transform.position.y, transform.position.z + _touch.deltaPosition.x * (-_moveSpeed));
                //_rb.AddForce(new Vector3(transform.position.x, transform.position.y, transform.position.z + _touch.deltaPosition.x * (-_moveSpeed)));
                pos.z = Mathf.Clamp(transform.position.z, -_limitZ, _limitZ);
                transform.position = pos;
            }

        }
    }

    public void Movement()
    {
        transform.DOLocalMoveX(_stopPoint - 5.5f, _cycleLength);
    }

Even when moving strictly by Rigidbody forces, it’s possible for the physics engine to miss a collision. A common occurrence is a small object intersecting with a plane. If in one frame, the object is on one side of the plane, and the next frame, the object is on the other side of the plane, then the physics system will miss the collision. This can be a big problem if your game has a low frame rate, which in general most mobile games will have.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms