coding_giants

L15 - PongPaddle

Mar 26th, 2024
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PongPaddle : MonoBehaviour
  6. {
  7.     //paddle speed
  8.     public float speed = 5f;
  9.    
  10.  
  11.     void Update()
  12.     {
  13.         Move();
  14.     }
  15.  
  16.     //movement function
  17.     void Move()
  18.     {
  19.  
  20.         if(gameObject.name == "Paddle1")
  21.         {
  22.             //Getting information from up-down arrows or w-s keys to check which direction are we moving to
  23.             //GetAxisRaw returns only -1, 0, 1 so the speed will be constant
  24.             float y = Input.GetAxisRaw("Vertical");
  25.  
  26.             //calculating speed, direction and elapsed time
  27.             float speddDir = y * speed * Time.deltaTime;
  28.  
  29.             //changing the position of the paddle
  30.             transform.position += new Vector3(0, speddDir, 0);
  31.         }
  32.  
  33.         if (gameObject.name == "Paddle2")
  34.         {
  35.             float y = Input.GetAxisRaw("Vertical2");
  36.             float speddDir = y * speed * Time.deltaTime;
  37.             transform.position += new Vector3(0, speddDir, 0);
  38.         }
  39.  
  40.  
  41.     }
  42. }
  43.  
Add Comment
Please, Sign In to add comment