I’m trying to make the paddle control the direction the ball bounces along the x axis. i’ve played block breakers where this is possible by moving the paddle to the right or left just as the ball hits it.
this will upgrade the paddle from a “wall” to a " bat or racket "
I’m completely blank… any help will be appreciated
Hello Raptoeagle, how are you?
Ben/Sam suggests changing the form of the paddle, making the ball bounce sideways if it hits the side of the paddle. But if you want to make the paddle velocity apply to the ball you will have to make it inside a script within a OnCollision2D method, by increasing the ball.GetComponent().velocity.x by the paddle.GetComponent().velocity.x*multiplier, let me know if you need further help doing that
Hello Joao
thanks for the reply!
i have modified the shape of the paddle 2D collider and even reduced the size of the ball so the angled sides of the collider will have more surface area to affect the ball… (can’t really see if it helps in the game)
Your suggestion about modifying the ball velocity by increment it with the paddle velocity is very nice. i tried to figure out how to do it myself but i can’t figure it out…even with the pointers you gave. I am slowly understanding the “logic” part of coding but i still can write the “language”
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
// Use this for initialization
void Start () {
paddle = GameObject.FindObjectOfType<Paddle> ();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
if (!hasStarted) {
// Lock the ball relative to the paddle.
this.transform.position = paddle.transform.position + paddleToBallVector;
// Wait for a mouse press to launch.
if (Input.GetMouseButtonDown(0)) {
print ("Mouse clicked, launch ball");
hasStarted = true;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2 (3f, 12f);
}
}
}
void OnCollisionEnter2D (Collision2D collision) {
Vector2 tweak = new Vector2(Random.Range (0f, 0.5f), Random.Range(0f, 0.3f));
// i guess this is where i'm supposed to add the modification.
if (hasStarted) {
GetComponent<AudioSource>().Play();
gameObject.GetComponent<Rigidbody2D>().velocity += tweak;
}
}
}
this is my ball code, it’s not different from the original. i’ll need your assistance in implementing your suggestion.
Hello Raptoreagle,
Sure, something like this should work:
void OnCollisionEnter2D (Collision2D collision) {
Vector2 tweak = new Vector2(Random.Range (0f, 0.5f), Random.Range(0f, 0.3f));
// i guess this is where i'm supposed to add the modification.
if (hasStarted) {
GetComponent<AudioSource>().Play();
gameObject.GetComponent<Rigidbody2D>().velocity += tweak;
}
if (collision.gameObject.tag == "Paddle")
{
gameObject.GetComponent<Rigidbody2D>().velocity += collision.gameObject.GetComponent<Rigidbody2D>().velocity * multiplier;
}
}
You would have to set an value to the variable multiplier in order to tweak how much it should add to the ball velocity and set the paddle tag to “Paddle”
There may be a problem with this method, I think that the way the game was done the paddle probably don’t have an velocity since it is being moved manually, perhaps you will to hard code it within a public variable inside the paddle that takes the transform.position on t and compares it to the transform.position on t-1