- Home /
Paddle script for Pong-based video game for Android
Hi there, I am trying to finish my pong based video game for Android devices, but I am stuck with the Paddle script. This script is the same for Paddle1 and Paddle 2. What I want to achieve is to drag the bar in the Z axis when it is touched and leave in the position where the bar is when the touch has ended, and also what I need is to detect two touches: one for Paddle1 and other for Paddle2. This is the code I have right now:
public static int changeDirection1;
public static int changeDirection2;
void Start () {
changeDirection1 = 1;
changeDirection2 = 1;
}
void Update () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
float touchDeltaPositionZ = Input.GetTouch(0).deltaPosition.y;
float position = this.transform.position.z;
if (gameObject.CompareTag ("PlayerBar")) {
if (position > -4.15f && position < 4.15f) {
transform.Translate (0, 0, touchDeltaPositionZ * Time.deltaTime * changeDirection1);
} else if (position <= -4.15f) {
if (touchDeltaPositionZ > 0) {
transform.Translate (0, 0, touchDeltaPositionZ * Time.deltaTime * changeDirection1);
}
} else if (position >= 4.15f) {
if (touchDeltaPositionZ < 0) {
transform.Translate (0, 0, touchDeltaPositionZ * Time.deltaTime * changeDirection1);
}
}
}
if (gameObject.CompareTag ("Player2Bar")) {
if (position > -4.15f && position < 4.15f) {
transform.Translate (0, 0, touchDeltaPositionZ * Time.deltaTime * changeDirection2);
} else if (position <= -4.15f) {
if (touchDeltaPositionZ > 0) {
transform.Translate (0, 0, touchDeltaPositionZ * Time.deltaTime * changeDirection2);
}
} else if (position >= 4.15f) {
if (touchDeltaPositionZ < 0) {
transform.Translate (0, 0, touchDeltaPositionZ * Time.deltaTime * changeDirection2);
}
}
}
}
if (GameController.twoPlayers) {
if (p1bar == false && CompareTag ("PlayerBar") && FootballPlayers.bar1Hitted == false) {
changeDirection1 = 1;
} else if (p1bar == true && CompareTag ("PlayerBar") && FootballPlayers.bar1Hitted) {
changeDirection1 = -1;
}
if (p2bar == false && CompareTag ("Player2Bar") && FootballPlayers.bar2Hitted == false) {
changeDirection2 = 1;
} else if (p2bar == true && CompareTag ("Player2Bar") && FootballPlayers.bar2Hitted) {
changeDirection2 = -1;
}
}
Your answer
Follow this Question
Related Questions
How could I implement diagonal movement in this code? 0 Answers
How do I move an object with my finger?[C#] 1 Answer
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
different devices -> Different touch speed 3 Answers
Raycasting UI elements - Without using Event Triggers 0 Answers