- Home /
Question by
breathinnowbreathout · Jun 15, 2020 at 09:36 AM ·
2dscripting problemmovement
Object some times goes off screen
I'm trying to create a pong copy and the ball some times goes off the screen edges. Been trying to use Mathf.Clamp but the ball just gets stuck on the sides of the screen with it. Here's my code for the ball:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class Ball : MonoBehaviour
{
//Speed variable
public float speed = 15f;
//Variables for moving horizontaly and verticaly
public float directionx = -1;
public float directiony = 0;
GameObject lRacket;
GameObject rRacket;
// Start is called before the first frame update
void Start()
{
//Find Rackets
lRacket = GameObject.Find("LeftRacket");
rRacket = GameObject.Find("RightRacket");
}
// Update is called once per frame
void Update()
{
//Bounce off screen edges
if (transform.position.y > 4.7)
{
directiony = -directiony;
}
else if (transform.position.y < -4.7)
{
directiony = -directiony;
}
if (transform.position.x < -6.5)
{
directionx = -directionx;
}
else if (transform.position.x > 6.5)
{
directionx = -directionx;
}
//Moves the ball
transform.Translate(new Vector3(directionx, directiony, 0) * speed * Time.deltaTime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider == true)
{
//Changes horizontal direction when it hits a racket
directionx *= -1;
//Moves up if it hits the upper side of the left racket
if (transform.position.x < 0f && transform.position.y > lRacket.transform.position.y)
{
directiony = RandomY();
}
//Moves down if it hits the lower side of the left racket
else if (transform.position.x < 0f && transform.position.y < lRacket.transform.position.y)
{
directiony = -RandomY();
}
//Moves up if it hits the upper side of the right racket
else if (transform.position.x > 0f && transform.position.y > rRacket.transform.position.y)
{
directiony = RandomY();
}
//Moves down if it hits the lower side of the right racket
else if (transform.position.x > 0f && transform.position.y < rRacket.transform.position.y)
{
directiony = -RandomY();
}
}
}
float RandomY()
{
//Randomizes how far up or down the ball will move after colliding with the racket
return Random.Range(0.1f, 0.7f);
}
}
and here's what I tried to do with Math.Clamp:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallStaysInScreen : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -6.5f, 6.5f),
Mathf.Clamp(transform.position.y, -4.7f, 4.7f), transform.position.z);
}
}
How do I solve this and why does the ball go off screen?
Comment
Your answer
Follow this Question
Related Questions
My Touch Movement Script is Not Working Correctly 1 Answer
Moving player to mouse click 2 Answers
Why are my controls laggy 1 Answer
2D Movement and getting the mouse position 0 Answers
Help with rotating a sprite with mouse. 2 Answers