Untitled
unknown
plain_text
a year ago
1.1 kB
4
Indexable
Never
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Ball : MonoBehaviour { // Start is called before the first frame update private float xPosition = 0f; private float yPosition = 0f; private float xSpeed = 0f; private float ySpeed = 0f; // Limit the ball to the edges of the screen private float xMin = -10.0F; private float xMax = 10.0F; private float yMin = -5.0F; private float yMax = 5.0f; void Start() { } // Update is called once per frame void Update() { // 1. check if the ball has reached eges of screen if (xPosition < xMin || xPosition > xMax) { xSpeed = -xSpeed; } if (yPosition < yMin || yPosition > yMax) { ySpeed = -ySpeed; } // 2. update the position of the ball xPosition = xPosition + xSpeed; yPosition = yPosition + ySpeed; transform.position = new Vector2(xPosition, yPosition); } }