How to move paddle around by sliding on touch screen?

Hi!

Iam making a BlockBreaker remake based on the things I have learned here. But Iam making an Android build, so the way paddle is moved around has to be adjusted.

I have decided that instead of making buttons for the left and right movement, I want to be able to touch the paddle on the screen and move it by sliding.

So far I’ve googled up some code that Iam using for this matter, but it doesn’t work the way I want it to work. Iam having different problems the moment I try adjusting it to my needs. And my needs are that the paddle moves only in the x direction and that it moves only to a certain point.

Uploading…

My code right now is this:

    public float padding;

    private float minX;
    private float maxX;
    

    void Start () {
        float distance = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftMost = Camera.main.ViewportToScreenPoint(new Vector3(0f, 0f, distance));
        Vector3 rightMost =  Camera.main.ViewportToScreenPoint(new Vector3(1f , 0f, distance));
        minX = leftMost.x + padding;
        maxX = rightMost.x - padding;    
    }
    
    void Update() {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {   
            float touchDeltaPosition = Mathf.Clamp(Input.GetTouch(0).deltaPosition.x, minX,maxX);
            transform.position = new Vector3(touchDeltaPosition, transform.position.y, transform.position.z);
        }
    }
}

I have juggled around with the code, using transform.position and transform.Translate and in both cases the paddle just doesnt do Jack. It keeps on dissapearing and appearing or it jumps to the right (over the padding) or back on the starting position. I noticed that it reacts only to sliding that goes from left to right, and never the other way around. All sorts of wierd stuff, no progress.

What is confusing me the most is that if I take the code from Unity documents (the first example here), it’s moving the paddle around just okay - in all directions. When I change it so that it would only work in the x direction like this:

        float touchDeltaPositionX = Input.GetTouch(0).deltaPosition.x;

        // Move object across XY plane
        transform.Translate(-touchDeltaPositionX , this.transform.position.y, 0);

it stops working. The paddle only starts blinking on the starting spot, nothing else. Can somebody please help me with this, its quite confusing to say the least.

Thank you!

So far this helped and its working nice and smooth:

    void Update() {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {                
            float touchDeltaPositionX = Input.GetTouch(0).deltaPosition.x;
            float touchDeltaPositionY = this.transform.position.y;
            transform.Translate(touchDeltaPositionX * speed, touchDeltaPositionY * 0, 0);
        }
    }

Now I only need to figure out an alternative way to restrict paddle’s movement. Mathf.Clamp messes up things if I use it the way I did in the first post because as I understand the deltaPosition returns the lenght of the vector as number of pixels. That doesnt sit well with minX and maxX that are floats, both with values only between 0 and 1.

Solutions for this are still welcomed :slight_smile:

Is there no way to turn your min and max into floats or the deltaPosition to float?

Now that I’ve worked on this some more I realized that the problem is a bit more complex (what a surprise).

The only way I could restrict the movement so far (and it’s a flawed solution - I’ll explain in a second)
is this:

    void Update() {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
            float touchDeltaPositionX = Input.GetTouch(0).deltaPosition.x;
            float position = this.transform.position.x;  
      
            if(position > -4f && position < 4f){            
                float touchDeltaPositionY = this.transform.position.y;
                transform.Translate(touchDeltaPositionX * speed, touchDeltaPositionY * 0, 0);
            }
        }
    }

That if statement doesn’t help, it prevents the paddle from moving beyond a certain point. But the problem here is that it also dissables it when it gets there. The paddle stops working beyond this point and becomes “unreachable”.

Okay. And the confusing part about this is that when using GetTouch(0).deltaPosition I get back a value in pixels that has nothing to do with global position (if Iam wrong correct me please). Its just lenght of how much finger has moved on the screen (in pixels), so confining that is most probably not going to work.

When I use the transform.Translate method, it just automaticaly adds the value of the movement (that it gets from GetTouch(0).deltaPosition) to the position of the game object. So Iam kind of in a loop here. I can’t (or don’t know how to) restrict the amount of input. If I try to restrict the position value of the go on the other hand, I get the above result. Dissfunctional paddle. Who wants a dissfunctional paddle?

Got it… This is probably not the most glamorous bit of code here, but Iam learning. If anyone has an alternative way of doing this, it’s still welcomed.

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

            float touchDeltaPositionX = Input.GetTouch(0).deltaPosition.x;
            float position = this.transform.position.x;    
            float touchDeltaPositionY = this.transform.position.y; 
   
            if(position > -4f && position < 4f){
                    transform.Translate(touchDeltaPositionX * Time.deltaTime * speed, touchDeltaPositionY * 0, 0);
            }else if (position <=-4f){
                if(touchDeltaPositionX > 0){
                    transform.Translate(touchDeltaPositionX * Time.deltaTime * speed, touchDeltaPositionY * 0, 0);
                }
            }else if (position >=4f){
                if(touchDeltaPositionX < 0){
                    transform.Translate(touchDeltaPositionX * Time.deltaTime * speed, touchDeltaPositionY * 0, 0);
                }
            }
        }
    }
1 Like

Nice coding, congratulations! I still have to learn how to build to the mobile platform =/ never tried to.
By the way, I think that you don’t need the touchDeltaPositionY, you could probably delete it.

Hey, thanks for the comment Joao.

I did try without that line of code and instead used the this.transform.position.y or just zero in the arguments for the transform.Tanslate but it didn’t work. At least than it didn’t. This could surely be improved upon but right now Iam happy it works the way It was supposed to so I can make further progress with my game and devote time to the making of sprites and other stuff that awaits me (some physics still needed for the flowers, further developent of the content, more levels, etc…).

1 Like

The important thing is that it is working!

It is very refreshing when we write a code that works exactly as it was supposed to!!

1 Like

Privacy & Terms