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.