Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to achieve ping pong style paddle movement

Discussion in 'Scripting' started by dannu123, Jan 18, 2021.

  1. dannu123

    dannu123

    Joined:
    Sep 1, 2017
    Posts:
    7
    I'm making a mobile game with ping pong style mechanics. I managed to get the paddle to follow the players finger, but I'm having some issues with rotating it. I want the paddle to slightly rotate in the direction swiped and return to the original rotation once they stop swiping. I tried a few different things but none of them seemed to work or I just don't know how to apply them.

    Code (CSharp):
    1.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    2.         {
    3.  
    4.             endPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0.7f));
    5.  
    6.             spawnedPaddle.transform.rotation = LookAt.Quaternion.Euler.(0f, -Mathf.PingPong(Time.time * rotateSpeed, 90f), 0f);
    7.  
    8.             spawnedPaddle.transform.position = endPos;
    9.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Sounds like maybe you need a currentRotation and a desiredRotation to control the paddle angle, and to:

    1. set desiredRotation according to whatever swipe is going on
    2. tween currentRotation smoothly to the desiredRotation so the paddle doesn't jerk.

    #1 above is up to you: usually you compare position this frame to the position last frame to decide motion of a given finger, and from that value decide what desiredAngle you want.

    Here is the general way to do #2 above:

    Smoothing movement between discrete values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    The code: https://pastebin.com/ePnwWqnM
     
    dannu123 likes this.
  3. dannu123

    dannu123

    Joined:
    Sep 1, 2017
    Posts:
    7
    I just turned off my pc for the night but I'll be trying this out first thing tomorrow. Thanks for the detailed reply!
     
    Kurt-Dekker likes this.
  4. dannu123

    dannu123

    Joined:
    Sep 1, 2017
    Posts:
    7
    I tried this out, but I'm still not quite sure how I'm supposed to apply all of this. The paddle should gradually rotate to a maximum of 45 degrees from its original orientation in whatever direction the movement or swipe occurs. Once the player stops swiping, the paddle should return to its original orientation. I'm quite new to coding and Quaternions/rotations are still very alien to me (as you probably noticed lol). I know I'm asking for a lot, but could you give me an example that I could apply to my code on implementing this?

    Code (CSharp):
    1.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && !hasSpawned)
    2.         {
    3.             Touch touch = Input.GetTouch(0);
    4.  
    5.             touchTimeStart = Time.time;
    6.  
    7.             startPosScreen = Camera.main.ScreenToWorldPoint (new Vector3 (touch.position.x, touch.position.y, 0.7f));
    8.             lastPos = touch.position;
    9.  
    10.             spawnedPaddle = Instantiate(paddle, startPosScreen, Quaternion.Euler(90, 0, 0));
    11.  
    12.             hasSpawned = true;
    13.  
    14.         }
    15.  
    16.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    17.         {
    18.             Touch touch = Input.GetTouch(0);
    19.             newPos = touch.position;
    20.             direction = newPos - lastPos;
    21.             lastPos = newPos;
    22.             print(direction);
    23.  
    24.             endPosScreen = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0.7f));
    25.  
    26.             //currentRotation = Mathf.MoveTowards(currentRotation, desiredRotation, rotationSpeed * Time.deltaTime);
    27.  
    28.             spawnedPaddle.transform.Rotate(touch.deltaPosition.y , 0f, touch.deltaPosition.x );
    29.  
    30.             spawnedPaddle.transform.position = endPosScreen;
    31.         }
    I was just messing around here trying to figure out how these different rotation methods work.
     
  5. dannu123

    dannu123

    Joined:
    Sep 1, 2017
    Posts:
    7
    After playing around with the different rotation methods, I managed to get it working with these 2 lines of code. I have no idea if this is the ideal way to go about, but it works so I'll take it :D
    Code (CSharp):
    1.             paddleRot = Quaternion.Euler(-(touch.deltaPosition.y * rotationSpeed) + 90, 0 , -(touch.deltaPosition.x * rotationSpeed));
    2.  
    3.             spawnedPaddle.transform.rotation = paddleRot;
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I do not have such an example handy but the pastebin tweening code I posted above can easily be adapted. You would use the currentQuantity to drive a rotation:

    Code (csharp):
    1. var TheObjectTransform.rotation = Quaternion.Euler( 0, 0, currentQuantity);
    That will rotate it around the Z axis. If you're going to programmatically do rotations, understanding at least how to create rotations out of the Quaternion.Euler() factory function is not really optional.

    Well that's awesome... ship it out to the customer now then! :)
     
    dannu123 likes this.
  7. dannu123

    dannu123

    Joined:
    Sep 1, 2017
    Posts:
    7
    Ok, thanks again and I won't be shipping this out any time soon as I'm just getting started :D
     
    Kurt-Dekker likes this.