Okay, I’m making a Pong game. I am doing most of the code from scratch. At the moment there is only one Paddle. The issue is, the ball doesn’t seem to register when it hits the Paddle. Here is my code for the ball:`using UnityEngine;
using System.Collections;
public class BounceScript : MonoBehaviour {
public float xPosition = -9;
public float xDiffer = 0;
public float latSpeed = 10.0f;
public float vertSpeed = 10.0f;
public float yBound = 10.0f;
public float xBound = 10.0f;
PlayerControl reference;
private Vector2 vel;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
//reference = GameObject.Find ("PlayerSprite").GetComponent<PlayerControl>().yPos;
}
// Update is called once per frame
void Update () {
var pos = transform.position;
if (pos.y > yBound) {
pos.y = yBound;
vertSpeed = -vertSpeed;
}else if (pos.y < -yBound) {
pos.y = -yBound;
vertSpeed = -vertSpeed;
}
if (pos.x > xBound) {
pos.x = xBound;
latSpeed = -latSpeed;
}else if (pos.x < -xBound) {
pos.x = -xBound;
latSpeed = -latSpeed;
}
if (pos.y >= (PlayerControl.yPos - .48) && pos.y <= (PlayerControl.yPos + .48)) {
Debug.Log("Y Accurate");
if (pos.x == (-8.25)) {
Debug.Log ("Player Hit");
}
}
transform.position = pos;
vel = rb2d.velocity;
vel.y = vertSpeed;
vel.x = latSpeed;
rb2d.velocity = vel;
}
}
And for the Paddle:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
static public float xPos;
static public float yPos;
public KeyCode upKey = KeyCode.W;
public KeyCode downKey = KeyCode.S;
public float speed = 10.0f;
public float yBound = 10.0f;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
var vel = rb2d.velocity;
if (Input.GetKey (upKey)) {
vel.y = speed;
}else if (Input.GetKey (downKey)) {
vel.y = -speed;
}else if (!Input.anyKey) {
vel.y = 0;
}
rb2d.velocity = vel;
var playerPos = transform.position;
if (playerPos.y > yBound) {
playerPos.y = yBound;
}else if (playerPos.y < -yBound) {
playerPos.y = -yBound;
}
transform.position = playerPos;
yPos = playerPos.y;
xPos = playerPos.x;
}
}
Were as I get “Y accurate” consistently, “Player Hit” does not get triggered once. Any advice would be appreciated.