Godot Version 3.5
Question
I am trying to create the classic ping-pong game. Everything seems good except for the paddle.
A brief about paddle : It is a staticbody2d. That moves up and down.
Here’s the code i wrote:
using Godot;
using System;
public class StaticBody2D2 : StaticBody2D
{
private int speed = 400;
public Vector2 ScreenSize;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
ScreenSize = GetViewportRect().Size;
}
// // Called every frame. ‘delta’ is the elapsed time since the previous frame.
public override void _Process(float delta)
{
var velocity = Vector2.Zero;
if (Input.IsActionPressed(“ui_up”))
{
velocity.y -= 100;
}
if (Input.IsActionPressed(“ui_down”))
{
velocity.y += 100;
}
Position += velocity * (float)delta;
Position = new Vector2(
x: Mathf.Clamp(Position.x, 0, ScreenSize.x),
y: Mathf.Clamp(Position.y, 0, ScreenSize.y)
);
}
}
The clamping only seems to be working at the top but at the bottom, the paddle slides out off the screen.
Also i’ve added staticbodies as wall around the game. The ball which is a kinematicbody2d seems to be bouncing perfectly off the wall.
Can you help clamping the paddle inside of the playscreen.
Also btw i’m using godot.3.5.
sorry