Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PaddleScript : MonoBehaviour
- {
- [SerializeField]
- bool isPlayerTwo;
- [SerializeField]
- float speed = 0.2f;
- Transform myTransform;
- Rigidbody2D rb2D;
- int direction = 0; // 0 = still, 1= up, -1 = down
- float previousPositionY;
- void Start()
- {
- myTransform = transform;
- previousPositionY = myTransform.position.y;
- rb2D = GetComponent<Rigidbody2D>();
- }
- void FixedUpdate()
- {
- //if (isPlayerTwo)
- //{
- // if (Input.GetKey("o"))
- // MoveUp();
- // else if (Input.GetKey("l"))
- // MoveDown();
- //}
- //else
- //{
- // if (Input.GetKey("q"))
- // MoveUp();
- // else if (Input.GetKey("a"))
- // MoveDown();
- //}
- Vector3 targetVelocity = Vector3.zero;
- if (Input.GetKey("o")) { targetVelocity.y = 1; }
- else if (Input.GetKey("l")) { targetVelocity.y = -1; }
- targetVelocity = targetVelocity.normalized * speed;
- rb2D.MovePosition(transform.position + targetVelocity * Time.deltaTime);
- if (previousPositionY < myTransform.position.y)
- direction = -1;
- else if (previousPositionY > myTransform.position.y)
- direction = 1;
- else
- direction = 0;
- }
- void OnCollisionExit2D(Collision2D other)
- {
- if (other.transform.name == "Ball")
- {
- float adjust = 5 * direction;
- other.rigidbody.velocity = new Vector2(other.rigidbody.velocity.x, other.rigidbody.velocity.y + adjust);
- }
- }
- void LateUpdate()
- {
- previousPositionY = myTransform.position.y;
- }
- //void MoveDown()
- //{
- // myTransform.position = new Vector2(myTransform.position.x, myTransform.position.y - speed);
- //}
- //void MoveUp()
- //{
- // myTransform.position = new Vector2(myTransform.position.x, myTransform.position.y + speed);
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement