Control Ball velocity Angle

In similar android games, if the ball landed on either side of the paddle, the angle of the ball velocity changes.

This is done at the end of the project by adjusting the shape of the collider to be angled on either side to cause it to bounce away in a non liner way.

1 Like

Hello Fadi,

In addition to the info Martyn has provided, you may also want to have a look at this;

Hope this helps :slight_smile:

It didn’t work, but its ok. But I\f I wanted to build the game on android and IOS, are there any changes needed to be made?

Hi Fadi,

What didn’t work?

The paddle impact point. I copied it and pasted onto my paddle script, but there wasn’t any noticeable change.

Hello Fadi,

Did you change the name of the variable for the paddle? In the example script I linked to it is named _paddle but in your own script you may have the paddle variable called something else, paddle for example.

The script itself isn’t going to just make your ball rebound in a different direction, it’s just a starting point for you to build upon in order to achieve what you want to achieve.

There is an error saying “An object reference is required to access non-static member” (referring to transform) in this line
Vector3 paddleLocalImpactPoint = Paddle.transform.InverseTransformPoint(impact.point);

Could you copy/paste your code please.

using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {

	// Use this for initialization
	private void OnCollisionEnter2D(Collision2D collision)
{
    foreach (ContactPoint2D impact in collision.contacts)
    {
        if (collision.gameObject.name == "Paddle")
        {
            Vector3 paddleLocalImpactPoint = Paddle.transform.InverseTransformPoint(impact.point);

            Debug.Log("Paddle Impact\n\n" +
            "World: X = " + impact.point.x.ToString() + " Y = " + impact.point.y.ToString() + "\n" +
            "Local: X = " + paddleLocalImpactPoint.x + " Y = " + paddleLocalImpactPoint.y + "\n" +
            "\n-----------------------\n");
        }
    }
	}	
	// Update is called once per frame
	void Update () {
		Vector3 paddlePos= new Vector3 (0.5f, this.transform.position.y,0f);

		float mousePosInBlocks = Input.mousePosition.x / Screen.width *16;

		paddlePos.x = Mathf.Clamp(mousePosInBlocks,0.5f,15.5f);

		this.transform.position = paddlePos;
	}
}

Hello Fadi,

The code is intended to go onto the Ball GameObject, as it is that which you are bouncing in a different direction.

If you don’t have a reference in code to your Paddle GameObject within Ball.cs you will need to create one and then use this variable name as mentioned earlier.

Something along the lines of;

using UnityEngine;

public class Ball : Monobehaviour
{
    private Paddle paddle;

    private void Start()
    {
        paddle = GameObject.FindObjectOfType<Paddle>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        foreach (ContactPoint2D impact in collision.contacts)
        {
            if (collision.gameObject.name == "Paddle")
            {
                Vector3 paddleLocalImpactPoint = paddle.transform.InverseTransformPoint(impact.point);

                Debug.Log("Paddle Impact\n\n" +
                "World: X = " + impact.point.x.ToString() + " Y = " + impact.point.y.ToString() + "\n" +
                "Local: X = " + paddleLocalImpactPoint.x + " Y = " + paddleLocalImpactPoint.y + "\n" +
                "\n-----------------------\n");
            }
        }
    } 

    // ... the rest of the ball.cs code   
}

Also, please use the code formatting characters when copy/pasting code, it makes it a lot easier for people to read. Thanks.


See also;

A post was split to a new topic: Applying in-game currency

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms