This post deals with creating an arkanoid or breakout paddle that can be changed in size.
Creating an arkanoid / breakout racket
Create an empty GameObject in the hierarchy window with "Create Empty" that you call DynamicPaddle.
In the project window, create a C # script that you will call a paddle.
Add it to the Dynamic Paddle. Open the window in the script editor. By default, this is Visual Studio.
The objective is the following script:
public class paddle: MonoBehaviour states that everyone can access it.
{<–The statement begins
float <- Indicates numbers
minSizes and maxSize <- define the minimum and maximum size of the club
resizeSpeed <- the speed at which the club changes its size
positionY <- position of the paddle
SpriteRenderer <–Makes you see the paddle. The Sprite renderer you add via Unity under AddComponent. Sprite is the associated sprite.
public -> important for Unity - camera -> data type -> variable name via which the camera is available
myCamera -> stands for any camera selected
void Start () -> Subroutine that is called when the object that creates the script is created - Subroutine begins
void Update () -> subroutine is called in every frame
Cursor.visible = false <- cursor becomes invisible
sprite = GetComponent (); <- The program gets the SpriteRenderer from the Inspector window in Unity or from the object to display the Sprite, so the component of the object is used
sprite.size = new Vector2 (7.5f, sprite.size.y);
sprite.size <- size of the sprite
new Vector2 (7.5f, sprite.size.y); <- new vector, which is composed of the x and y values - the x value is the initial variable and the y variable is retained
positionY = -myCamera.orthographicSize + (sprite.size.y * 0.5f);
positionY = position of the paddle on the Y-axis
-myCamera.orthographicSize <- Reference to the camera and the size of the camera
Since the origin of our game is in the center of the camera, when calculating the Y position we have to subtract half the camera size (now the paddle would be on the lower edge of the screen) and add the height of the paddle to it.
float sizeX = sprite.size.x; <- The size of the paddle and sprite size are identical
sizeX + = Input.GetAxisRaw ("Mouse ScrollWheel") * resizeSpeed; <–The size X is defined by x = x + 1 and is influenced by the mouse wheel in size, which is multiplied by the speed with which the paddle changes size
sizeX = Mathf.Clamp(sizeX, minSize, maxSize);
The size X is fixed with the size X between the smallest and the largest value. The value can never be smaller or larger than the min. And max. Value.
sprite.size = new Vector2(sizeX, sprite.size.y);
The sprite size is defined by the new Vector2, which is made up of the x and y values of the sprite.
Vector 3 mousePosition = myCamera.ScreenToWorldPoint (Input.mousePosition);
Vector 3 is the mouse position. This corresponds to the camera position and how it is directed towards the game world. The camera is based on the mouse position. With the ScreenToWorldPoint, the Vector is transformed from the screen position (screen position) to the world position (position in the game world).
transform.position = newVector3 (mouseposition.x, positionY);
The change in the position of the paddle corresponds to Vector3, which is composed of mouse positions x and Y.
You also need the right sprite to which you attach the script. Then the paddle will appear on the screen.
Originally posted on 2019-11-23 21:45:00.