Search Unity

How do I create a pong paddle controller?

Discussion in 'Scripting' started by Deleted User, Feb 25, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hello. I am working on a pong game in 3d and I am having a bit of trouble creating a controller script for the paddle. I want to be able to attach it to a rigidbody and move it up an down the x axis using w and s. I tried messing around with my fps player controller script to get that effect but was unsuccessful. Any ideas on how to create such a script would be highly appreciated. Thanks.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'd think you would simply use Rigidbody.MovePosition to move the paddle to where you think it should be. (For example, its current position, plus some multiple of the Horizontal and Vertical input axes times Time.deltaTime.)
     
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    I'm a little confused.

    W and S are usually associated with Vertical Movement.
    You are discussing moving up and down the X axis ??
    Can you clarify?

    Also , did you want to limit the extent of movement ie using Mathf.Clamp ( ... ) ?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Instead of checking for W and S directly, you should check Input.GetAxis("Vertical"), which by default is mapped to both W/S and up/down arrows.

    So, I'm proposing something like this:

    Code (CSharp):
    1. Vector3 inp = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
    2. rigidbody.MovePosition(transform.position + inp * speed * Time.deltaTime);
     
  5. Deleted User

    Deleted User

    Guest

    Hello again. All I did was what Joe Strout recommended and used rigidbody.moveposition in my script.
    Code (JavaScript):
    1. var speedForward : Vector3 = Vector3 (3, 0, 0);
    2. var speedBackward : Vector3 = Vector3 (-3, 0 ,0);
    3.  
    4.  
    5. function Update () {
    6.    
    7.    
    8.     if(Input.GetKey(KeyCode.W))
    9.     {
    10.         rigidbody.MovePosition(rigidbody.position + speedForward * Time.deltaTime);
    11.     }
    12.        
    13.     if(Input.GetKey(KeyCode.S))
    14.     {  
    15.         rigidbody.MovePosition(rigidbody.position + speedBackward * Time.deltaTime);
    16.     }
    17. }
    I put two colliders at the sides of the board so the paddles or the puck wouldn't fly off.
     
    JoeStrout likes this.
  6. Deleted User

    Deleted User

    Guest

    Now how would I make the puck bounce off of the paddle. I thought about having three separate colliders on the paddle and having the middle one make the puck bounce forward and the left one bounce forward and right and so on. I think there might me a more efficient way to so so but I don't necessarily know how.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You could consider using a cylinder collider, scaled so that it's long and thin. But then it would still have some curvature, giving you more control over the puck.