Swiping a ball 3D

Hello,

I have a problem with a ball (sphere). the ball bounces up at times. But they shouldn’t. Here’s some code. Many thanks for the help!

void Update()
    {
      if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {

            // getting touch position and marking time when you touch the screen
            touchTimeStart = Time.time;
            startPos = Input.GetTouch(0).position;
        }
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {

            // marking time when you release it
            touchTimeFinish = Time.time;
                
            // getting release finger position
            endPos = Input.GetTouch(0).position;
      
            // calculating swipe direction
            direction = endPos - startPos;

            rb.isKinematic = false;
  rb.AddForce(direction.x * throwForceInXandY, direction.y * throwForceInXandY,0);

and here a hardcopy from the unity screen:

Hi,

If you add force in y, it will move up. It’s expected behavior.

Unfortunately, it still doesn’t work. The ball always moves in a different direction than it is pushed with the touch.
i have change the code:

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)       
       {

            touchTimeStart = Time.time;
            startPos = Input.GetTouch(0).position;
        }

     
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {
      
            touchTimeFinish = Time.time;
        
            timeInterval = (touchTimeFinish - touchTimeStart);
          
            endPos = Input.GetTouch(0).position;              
                  
            direction = endPos - startPos;        
               
            direction.Normalize();
            Kugel1.GetComponent<Rigidbody>().AddForce(direction*20);

It’s was in line 24 you were adding force in x, and y, but not z:
24. rb.AddForce(direction.x * throwForceInXandY, direction.y * throwForceInXandY,0);

Doesn’t the variables for the touch, endPos, and startPos just containts x, and y values too?