Block Breaker Paddle Control

So I’m trying to remake block breaker to test what I know but I can’t seem to figure out how to control the paddle with Horizontal.
I’ve tried using Rick’s code from Laser Defender but the paddle won’t move.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Paddle : MonoBehaviour
{

// configuration paramaters
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 15f;
[SerializeField] float screenWidthInUnits = 16f;

// cached references
GameSession theGameSession;
Ball theBall;

// Use this for initialization
void Start()
{
    theGameSession = FindObjectOfType<GameSession>();
    theBall = FindObjectOfType<Ball>();
}

// Update is called once per frame
void Update()
{
    Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
    paddlePos.x = Mathf.Clamp(GetXPos(), minX, maxX);
    transform.position = paddlePos;
}

private float GetXPos()
{
    if (theGameSession.IsAutoPlayEnabled())
    {
        return theBall.transform.position.x;
    }
    else
    {
        return Input.mousePosition.x / Screen.width * screenWidthInUnits;
    }
}

}

…using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BetterPlayerMove : MonoBehaviour
{

// configuration parameters

[SerializeField] float moveSpeed = 10f;
[SerializeField] float padding = 1f;



public GameObject destructionFX;

public static Player instance;



float xMin;
float xMax;
float yMin;
float yMax;
/*internal static object instance;*/

// Use this for initialization
// cached references
GameSession theGameSession;
Ball theBall;


void Start()
{
    SetUpMoveBoundaries();
    theGameSession = FindObjectOfType<GameSession>();
    theBall = FindObjectOfType<Ball>();
}

// Update is called once per frame
void Update()
{
    Move();
}



private void Move()
{
    var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
    var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

    var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
    var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
    transform.position = new Vector2(newXPos, newYPos);
}

private void SetUpMoveBoundaries()
{
    Camera gameCamera = Camera.main;
    xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
    xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
    yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
    yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
}

}…
I need to make this one code in which I can move the paddle on the X & still shoot the ball.

Hi davidwindsands,

Doesn’t the paddle move at all or doesn’t it move as expected? The difference is important. Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Are there any error messages in your console?

Also check the clamp values manually in your scene window by moving the paddle to the edges of your game screen, and check its position values in the Inspector.

i’m too young in the game to figure this out I’m gonna try a few things & keep going.

Yes, please do try a few things. That’s the best way to become familiar with Unity.

How are you getting on with this, @davidwindsands?

This topic was automatically closed after 6 days. New replies are no longer allowed.

Privacy & Terms