# bowcox

Member

7

0 Neutral

• Rank
Newbie

• Role
Programmer
• Interests
Art
Audio
Education
Programming

• Steam
Bowcox

## Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

1. ## Raycasting in 2D Pong

This does make a lot of sense, thanks. I have tried your way and I've tried adapting it to my situation as much as I can. The division of the distance travelled vertically by the play area height doesn't work if there is a bounce and vertical distance less than the play area height. I've tried patching it from top to bottom and it seems way too complicated compared to using raycasts. I've even had to add in a raycast to detect if there was a bounce if the distance is less than the play area height. However, I might be being stupid. Can I ask if you know why, in the above code I originally attached, there ends up being a loop from the middle if statement? The other two statements seem to work perfectly fine. I don't quite understand why just simply trying to add only two raycasts to the situation should be a problem..
2. ## Raycasting in 2D Pong

Ok, I see what you mean now with using the total height of the play area. I would never have thought to do that aha, thanks for the idea! I'll let you know how I get on with it. Out of curiosity though, with this below, because I just like to know What do you mean by it doesn't persist? You mean, it's job is done after it hits something and doesn't exist anymore? Then the information about the collision is stored within a variable? Sorry if I'm an idiot, I've looked elsewhere and I haven't managed to find many clear enough answers. I've learnt way more talking to you than I have the past 2 days looking to see how raycasts act. If I could make them just appear for a split second to simply understand whats happening, I would.
3. ## Raycasting in 2D Pong

Yeh, it's a constant speed with perfect collision. My bad. Do you mean finding the y-position of the top boundary and bottom boundary, using the velocity of the ball and initial collision point to calculate the path? I feel it may be slightly long winded for more than one collision but I'll give it a go. I just felt like using raycasts might have been easier and I wondered whether it was possible to do so? I'm curious to know how they work anyway for future reference. Like whether they move with the object, whether you can have two raycasts, or if you have to remove a raycast in order to produce another one (maybe because they're associated with the same object). Thanks for replying by the way Appreciate any help I can get
4. ## Pong AI

Above is a link to my new post, if you'd like to help, I would more than welcome it
5. ## Pong AI

Thanks very much for replying, I did try your method but have ended up trying to use raycasts. It's not been fun but I'm almost there. Thanks for your input though, it gave me something to try when I was struggling to think of anything else .
6. ## Raycasting in 2D Pong

Hi guys, I've never done any ray-casting before and have a problem using it in my game. I have managed to get the initial raycast to work and drawing rays is much easier now to me but if the ball were to bounce against the top of the bottom walls in my game of pong, I want to raycast from those points as quickly as possible to predict where it is going to end up on the computer side, my code for it is below.. I've tried to annotate to help demonstrate my problem. using System.Collections; using System.Collections.Generic; using UnityEngine; public class RaycastTest : MonoBehaviour { private Rigidbody2D rb2d; private RaycastHit2D hit; private Vector2 predictedCollision; private Vector2 collisionPoint; private int numberOfCollisions; void Start () { rb2d = GetComponent<Rigidbody2D> (); numberOfCollisions = 0; } void FixedUpdate () { //Directions of the ball (normalized) Vector3 forward3 = new Vector3 (rb2d.velocity.normalized.x, rb2d.velocity.normalized.y, 0) * 20; Vector2 forward2 = new Vector2 (rb2d.velocity.normalized.x, rb2d.velocity.normalized.y); // If the ball has just hit the player paddle if (BallMovement.isOnPath == true) { if (numberOfCollisions == 0) { //make sure we have the correct initial collision point from another script collisionPoint = BallMovement.collisionPoint; //draw and cast a ray to detect the next point of collision Debug.DrawRay (collisionPoint, forward3, Color.red, 20f); hit = Physics2D.Raycast (collisionPoint, forward2, Mathf.Infinity); //print the name of the object that has been hit print (hit.collider.name); //change the initial collision point to the new one collisionPoint = new Vector2 (hit.point.x, hit.point.y); print (collisionPoint); //add on to the number of collisions numberOfCollisions += 1; print (numberOfCollisions); } //if we still haven't reached the HitArea we need to do another bounce if (hit.collider.name != "HitArea") { //make sure we get the velocity after impact and reverse the direction of the y velocity. forward2 = new Vector2(rb2d.velocity.normalized.x, rb2d.velocity.normalized.y * -1f); forward3 = new Vector3 (rb2d.velocity.normalized.x, rb2d.velocity.normalized.y * -1f, 0) * 20; //draw and cast a ray to detect the next point of collision hit = Physics2D.Raycast(collisionPoint, forward2, Mathf.Infinity); Debug.DrawRay(collisionPoint, forward3, Color.red, 20f); //change the initial collision point to the new one collisionPoint = hit.point; print (collisionPoint); //add on to the number of collisions numberOfCollisions += 1; } //If we've hit the HitArea, we reset everything so it stops looping and we've got our predicted point of collision on the computer side. if (hit.collider.name == "HitArea") { predictedCollision = hit.point; numberOfCollisions = 0; BallMovement.isOnPath = false; } } } } My real question is, when you have a script attached to the ball, like this one, is the raycast attached and moving with the ball? Do I need to make a script on the side? Or can I remove the initial raycast attached and start a new one from the new collision point if it hits the top or bottom boundary? Thanks guys!
7. ## Pong AI

Hi guys! I have created a Pong game that has an AI that is almost beatable, changing the speed of the AI can make it ridiculously easy or hard depending on the way you go about it. using System.Collections; using System.Collections.Generic; using UnityEngine; public class ComputerMovement : MonoBehaviour { private float speed; private float reAdjustSpeed = 1f; private Rigidbody2D computer2d; public static bool isTwoPlayer; GameObject theBall; Rigidbody2D rb2d; void Start() { computer2d = GetComponent<Rigidbody2D> (); } void FixedUpdate() { if (isTwoPlayer == true) { speed = 5f; if (Input.GetKey (KeyCode.W)) { computer2d.position += Vector2.up * speed * Time.deltaTime; } if (Input.GetKey (KeyCode.S)) { computer2d.position += Vector2.down * speed * Time.deltaTime; } } if (isTwoPlayer == false) { speed = 3f; if (theBall == null) { theBall = GameObject.FindGameObjectWithTag ("Ball"); } rb2d = theBall.GetComponent<Rigidbody2D> (); //Is the ball going left or right if (rb2d.velocity.x > 0) { if (rb2d.velocity.y > 0) { if (rb2d.position.y > computer2d.position.y) { MoveUp (); } if (rb2d.position.y < computer2d.position.y) { MoveDown (); } } if (rb2d.velocity.y < 0) { if (rb2d.position.y > computer2d.position.y) { MoveUp (); } if (rb2d.position.y < computer2d.position.y) { MoveDown (); } } } //Whilst it's not moving at the paddle, let it gain a slight reset by moving with the ball at a slower pace. if (rb2d.velocity.x < 0) { if (computer2d.position.y < 0) { computer2d.position += Vector2.up * reAdjustSpeed * Time.deltaTime; } if (computer2d.position.y > 0) { computer2d.position += Vector2.down * reAdjustSpeed * Time.deltaTime; } } } } void MoveDown() { if (Mathf.Abs(rb2d.velocity.y) > speed) { computer2d.position += Vector2.down * speed * Time.deltaTime; } else { computer2d.position += Vector2.down * speed * Time.deltaTime; } } void MoveUp() { if (Mathf.Abs (rb2d.velocity.y) > speed) { computer2d.position += Vector2.up * speed * Time.deltaTime; } else { computer2d.position += Vector2.up * speed * Time.deltaTime; } } } I have looked up several posts across many different forums in order to create a much better AI. Most of the posts recommend that I use Raycasts to find out exactly where the ball might hit the paddle. I have looked up how to use them and I'm just completely lost, do raycasts consider collisions and go on infinitely or once they hit a wall, that's where it'll end up? Would anyone be able to help me understand raycasts a little better? If you have another solution that enables me to calculate exactly where the ball will end up on the opponents side, I am more than willing to hear it Thanks again if you read this!