Unity3D C# Drag object smoothly on it's x axis that's already bouncing

In my scene, I have a bouncing ball that collides with a cube. On each collision the ball returns to the same height. How can I get the ball to smoothly move along the x-axis by dragging my finger across the screen, while at the same time having no effect on the y or z values?

The y position is changing every frame depending on the state of the jump, so setting it's y position to transform.position.y in the Update function causes the movement to be jittery if the frame renders when it isn't caught up with the position of the ball.

Also, I set the interpolate value of the rigidbody to interpolate to get a smoother jumping motion, not sure if this is relevant to my problem or not.

Here is where I am at so far with little success. A few mobile games that replicate what I'm trying to achieve in terms of ball movement are Hop, Color Hop, and Splashy!

void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
// Control sliding using touch input.
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(touchDeltaPosition.x * slidingSpeed, 0, 0);
}
transform.position = new Vector3 (Mathf.Clamp(transform.position.x, - xPosLimit, xPosLimit), transform.position.y, transform.position.z);
}

• If I understood correctly then what you will want is the absolute position of the finger on the screen such that a touch on the left side of the screen will move the ball to the left, therefore the ball will essentially always be directly under your finger. Using the deltaposition will mean that the ball will only move when your finger changes position. So if you release your finger and place it down on one side of the screen the ball won't actually move. Does that sound about right or have I misunderstood? – mr-matt Nov 19 '18 at 3:17
• @mr-matt The ball should mirror the x-coordinate of the touch position regardless of the side of the screen that is touched. The only touch recognition I need to use is touches moved. I can get it do that with the code above but the y-value of the ball is jittery and has unexpected behavior. – Tiptonix Beats Nov 19 '18 at 3:58

You've almost got it. Given that you want the absolute position, I would switch from using the touch delta position to the touch position (absolute). I then normalized it and used your xPosLimit variable to determine the final xPosition.

Treat it as pseudocode tho, I wrote it from memory.

void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
// Control sliding using touch input.
Vector2 touchPos = Input.GetTouch(0).position;

//normalize the X coord (make it between 0 and 1), Y coord is irrelevant
float normalizedX = touchPos.x / Screen.width;

//interpolate between -xPosLimit and +xPosLimit by the normalizedX coord
//no need to clamp it
float xPos = Mathf.lerp(-xPosLimit, xPosLimit, normalizedX);

//set the new position, leave Y and Z alone
transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
}
}


Update: See comment below, the issue was solved with the above code AND by disabling interpolation on the rigidbody.

• The ball is doing the same thing. Mirroring the x-position properly, but the ball is super jittery and the y-value of the ball appears to glitch up and down. – Tiptonix Beats Nov 19 '18 at 4:51
• Can you add a screenshot or a video clip? Nothing in that code jumps out at me regarding jittery movement. – mr-matt Nov 19 '18 at 4:57
• Your code is performing as expected when there is no interpolation. Turns out the reason the ball was being jittery and moving erratically was because of the interpolation on the rigid body. Of course before that made the bouncing look smoother, but functionality outweighs perfectly smooth movement in this case I suppose. @mr-matt – Tiptonix Beats Nov 19 '18 at 6:19