- Home /
Pong's pad does not follow ball correctly
the problem i am facing is that i have a pong game. in that the pad controlled by the game is set by a simple line of code. it basically just translates its y position to the y position of the ball. but the problem is that it likes to strafe in the opposite direction for example if the ball's y position is 2, then it will first strafe to -1 before correcting it self
Game's pad's code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy : MonoBehaviour
{
public float distanceFromRight, speed;
public Transform ball;
private void Update()
{
movement();
}
private void movement()
{
transform.position = new Vector2(Mathf.Clamp(transform.position.x, -playerController.position, -playerController.position), transform.position.y);
if (Vector2.Distance(new Vector2(0, ball.transform.position.y), new Vector2(0, transform.position.y)) > .2f)
{
transform.Translate(new Vector2(0, ball.transform.position.y) * speed * Time.deltaTime, Space.Self);
Debug.Log("Ball transform.position.y is " + " ' " + ball.transform.position.y + " ' " + " while the pad's tranform.position.y is " + " ' " + transform.position.y + " while the distance between the two is equal to " + " ' " + Vector2.Distance(new Vector2(0, ball.transform.position.y), new Vector2(0, transform.position.y)) + " ' ");
} else
{
transform.position = transform.position;
}
}
}
while the ball's code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball : MonoBehaviour
{
private Rigidbody2D rb;
private Vector2 enemyPos, playerPos;
private bool isOnRight, isOnLeft;
public float speed;
public static Vector2 ballPosition;
public Transform enemy;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.right * -1 * speed;
}
private void OnCollisionEnter2D()
{
ballPosition = transform.position;
if (transform.position.x < 0)
{
enemyPos = enemy.transform.position;
isOnLeft = true;
isOnRight = false;
} else
{
playerPos = playerController.playerPosition;
isOnRight = true;
isOnLeft = false;
}
}
private void Update()
{
if (isOnRight)
{
transform.Translate(new Vector2(playerPos.x, -playerPos.y) * speed * Time.deltaTime, Space.World);
if (Vector2.Distance(new Vector2(transform.position.x, 0), new Vector2(playerPos.x, 0)) < .4f)
{
isOnRight = true;
isOnLeft = false;
}
}
if (isOnLeft)
{
transform.Translate(new Vector2(enemyPos.x, -enemyPos.y) * speed * Time.deltaTime, Space.World);
if (Vector2.Distance(new Vector2(transform.position.x, 0), new Vector2(enemyPos.x, 0)) < .4f)
{
isOnLeft = true;
isOnRight = false;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Vector2.Lerp not working properly. Horizontal jittering when not moving horizontally at all! 1 Answer
Is it possible to continue a method after Input? 2 Answers
How can I instantiate and move objects smoothly? 1 Answer
Question Local Translation PRoblem 2 Answers
Trying to translate a game object a fixed distance 3 Answers