Cube Move One Position To Another Position and when cube doesn't stop when hit collider??

cube move but doesn’t stop when hit collider??

Code:

public class MoveCube : MonoBehaviour
{
    float speed = 0.5f;
    public Animator anim;
    
    void Update()
    {
        foreach (Touch touch in Input.touches)
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition; //touch movement
                transform.Translate(touchDeltaPosition.x * speed * Time.deltaTime, touchDeltaPosition.y * speed * Time.deltaTime, 0);

                Ray ray = Camera.main.ScreenPointToRay(touch.position); 
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit, 1000))
                {
                    if (hit.collider.CompareTag("Environment"))//when hit my collider
                    {
                         Cubecall();  //when touch my collider then call a cubecall anim
                    }
                }
            }
        }
    }

    //void OnCollisionEnter2D(Collision2D collision)
    //{
    //   if(collision.gameObject.name=="Environment")
    //    {
    //        Cubecall();
    //    }
    //}

    void Cubecall()
    {
        anim.enabled = true;
    }
}

GameObjectCollider

136744-environmentgameobject.png

It’s not colliding because you’re using transform.Translate(), which just moves the object and doesn’t take into account any of the physics. Instead, you should try using Physics.AddForce() or directly change Rigidbody.velocity (and make sure you change your Update() function to a FixedUpdate() function if you do this as well).

Thanks for Reply…
can i store my cube move position in vector
can u edit my code

    void FixedUpdate()
    {
        foreach (Touch touch in Input.touches)
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition; //touch movement 
                Vector2 pos = touchDeltaPosition;
                pos.x = touchDeltaPosition.x * speed;
                pos.y = touchDeltaPosition.y * speed;
                transform.Translate(pos.x,pos.y,0);

                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                RaycastHit hit;
                  
               if (Physics.Raycast(ray, out hit, 1000))
                {
                    if (hit.collider.CompareTag("Environment"))//when hit my collider
                    {
                         Cubecall();  
                    }
                }
}