Here are the script of the paddle and script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
public float speed = 0.06f;
public float xBounds = 2.15f;
int direction = 0;
float previousPositionX;
private GameObject ball;
private float speedBall;
void Start()
{
ball = GameObject.Find("Ball");
}
void Update()
{
Movement();
}
void LateUpdate()
{
previousPositionX = transform.position.x;
}
void Movement()
{
float h = Input.GetAxisRaw("Horizontal");
if(h > 0)
{
transform.position = new Vector2(transform.position.x + speed,
transform.position.y);
}
else if (h < 0)
{
transform.position = new Vector2(transform.position.x - speed,
transform.position.y);
}
if (previousPositionX > transform.position.x)
direction = -1;
else if (previousPositionX < transform.position.x)
direction = 1;
else
direction = 0;
transform.position = new Vector2(Mathf.Clamp(transform.position.x,
-xBounds, xBounds), transform.position.y);
}
void OnCollisionEnter2D(Collision2D target)
{
float adjust = 3 * direction;
target.rigidbody.velocity = new Vector2(target.rigidbody.velocity.x + adjust,
transform.position.y);
}
}
here is the ball script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallScript : MonoBehaviour
{
public float speed, multiplier, constantSpeed;
public Transform startPoint;
private Rigidbody2D myBody;
private bool canMove, shoot;
void Awake()
{
myBody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (canMove)
{
if (!shoot)
{
shoot = true;
myBody.AddForce(new Vector2(0, speed * multiplier));
}
}
else
{
transform.position = startPoint.position;
}
if (Input.GetKeyDown(KeyCode.Space))
canMove = true;
myBody.velocity = constantSpeed * myBody.velocity.normalized;
}
}
I really dont know what to do.