i'm creating a pong game for learning purpose and decided to have the Paddle moving freely but with a slight change in its control.
When pressing LEFT OR RIGHT it should move in polar coordinates facing the ball. And UP OR DOWN moves the paddle towards or backwards the ball.
I've spent the whole day trying out a few solutions but didn't make it.
var ball = Vector3.zero; // target
var d = ball - translation.Value.ToVector3();
var radius = d.magnitude;
var dir = d.normalized * data.speed * deltaTime;
var angle = Vector3.Angle(ball, translation.Value);
var nextPos = translation.Value;
if (data.input.x > 0)
{
nextPos.x += dir.x + 15 * math.sin(angle) * data.speed * deltaTime;
nextPos.z += dir.z + 15 * math.cos(angle) * data.speed * deltaTime;
}
if (data.input.x < 0)
{
nextPos.x -= dir.x + 15 * math.sin(angle) * data.speed * deltaTime;
nextPos.z -= dir.z + 15 * math.cos(angle) * data.speed * deltaTime;
}
translation.Value = nextPos;