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);
}