Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PaddleScript : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     bool isPlayerTwo;
  9.     [SerializeField]
  10.     float speed = 0.2f;
  11.     Transform myTransform;
  12.     Rigidbody2D rb2D;
  13.  
  14.     int direction = 0; // 0 = still, 1= up, -1 = down
  15.     float previousPositionY;
  16.  
  17.     void Start()
  18.     {
  19.         myTransform = transform;
  20.         previousPositionY = myTransform.position.y;
  21.         rb2D = GetComponent<Rigidbody2D>();
  22.     }
  23.  
  24.     void FixedUpdate()
  25.     {
  26.         //if (isPlayerTwo)
  27.         //{
  28.         //    if (Input.GetKey("o"))
  29.         //        MoveUp();
  30.         //    else if (Input.GetKey("l"))
  31.         //        MoveDown();
  32.         //}
  33.         //else
  34.         //{
  35.         //    if (Input.GetKey("q"))
  36.         //        MoveUp();
  37.         //    else if (Input.GetKey("a"))
  38.         //        MoveDown();
  39.         //}
  40.         Vector3 targetVelocity = Vector3.zero;
  41.         if (Input.GetKey("o")) { targetVelocity.y = 1; }
  42.         else if (Input.GetKey("l")) { targetVelocity.y = -1; }
  43.         targetVelocity = targetVelocity.normalized * speed;
  44.         rb2D.MovePosition(transform.position + targetVelocity * Time.deltaTime);
  45.         if (previousPositionY < myTransform.position.y)
  46.             direction = -1;
  47.         else if (previousPositionY > myTransform.position.y)
  48.             direction = 1;
  49.         else
  50.             direction = 0;
  51.     }
  52.     void OnCollisionExit2D(Collision2D other)
  53.     {
  54.         if (other.transform.name == "Ball")
  55.         {
  56.             float adjust = 5 * direction;
  57.             other.rigidbody.velocity = new Vector2(other.rigidbody.velocity.x, other.rigidbody.velocity.y + adjust);
  58.         }    
  59.     }
  60.     void LateUpdate()
  61.     {
  62.         previousPositionY = myTransform.position.y;
  63.     }
  64.     //void MoveDown()
  65.     //{
  66.     //    myTransform.position = new Vector2(myTransform.position.x, myTransform.position.y - speed);
  67.     //}
  68.     //void MoveUp()
  69.     //{
  70.     //    myTransform.position = new Vector2(myTransform.position.x, myTransform.position.y + speed);
  71.  
  72.     //}
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement