- Home /
How do I get a sphere to bounce of a cube at an angle
I am trying to create a pong style game and I am having trouble getting my ball to bounce off the paddle at angles so that it doesn't just move up and down against the AI.
The ball has a sphere collider and rigidbody and the paddle just has the Box Collider and no rigid body, I'm not sure if I need to add one? Here is the code for the ball
#pragma strict
//Variable to hold paddle speed
var speed:int = 10;
function Update ()
{
if(Input.GetKey(KeyCode.Escape)){
Application.LoadLevel (0);
}
//player input
if(Input.GetButton("LEFT"))
{
transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
}
if(Input.GetButton("RIGHT"))
{
transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
}
if(transform.position.x <-7.88)
{
transform.position.x = -7.88;
}
if(transform.position.x >7.88)
{
transform.position.x = 7.88;
}
}
This for the paddle (Just trying to give you an idea of the setup.
#pragma strict
var hasreset = false;
var cSpeed:float = 10.0;
var sFactor:float = 10.0;
//two vars to hold score
static var playerScore:int = 0;
function Start ()
{
hasreset = true;
playerScore = 0;
yield WaitForSeconds (1.0);
rigidbody.AddForce(Random.Range(-5,5),0,0);
}
function resetball ()
{
yield WaitForSeconds (1.0);
GameObject.FindWithTag("paddle").transform.position.x = 0;
GameObject.FindWithTag("paddle2").transform.position.x = 0;
transform.position.x = 0;
transform.position.y = 0;
hasreset = true;
}
function takescore ()
{
yield WaitForSeconds (1.0);
playerScore --;
}
function addscore ()
{
yield WaitForSeconds (1.0);
playerScore ++;
}
function Update ()
{
OnTriggerEnter()
//this has something to do with smoothing (MATHS)
var cvel = rigidbody.velocity;
var tvel = cvel.normalized * cSpeed;
rigidbody.velocity = Vector3.Lerp(cvel,tvel,Time.deltaTime * sFactor);
//checking bounds for score and reset pos of ball and paddle
if(transform.position.y <-5.67)
{
resetball();
if(hasreset == true)
{
takescore();
}
hasreset = false;
}
if(transform.position.y >5.67)
{
resetball();
if(hasreset == true)
{
addscore();
}
hasreset = false;
}
}
Answer by robertbu · Apr 22, 2013 at 07:13 PM
There are syntax errors in the second script above. OnTriggEnter() is nested inside of Update(). And I don't see that the smoothing code is necessary. You have the scripts "named" backwards...the first script is for the paddle, the second for the ball. Is this your code or something you borrowed elsewhere?
One way that may get you what you want in a way that feels realistic is to impart part of the velocity of the paddle to the ball. See the OnCollisionExit() in the following modification of your code:
#pragma strict
var hasreset = false;
var cSpeed:float = 10.0;
var sFactor:float = 10.0;
//two vars to hold score
var frictionTransfer : float = 0.15;
static var playerScore:int = 0;
function Start ()
{
hasreset = true;
playerScore = 0;
yield WaitForSeconds (1.0);
//rigidbody.AddForce(Random.Range(-5,5),0,0);
}
function resetball ()
{
yield WaitForSeconds (1.0);
GameObject.FindWithTag("paddle").transform.position.x = 0;
GameObject.FindWithTag("paddle2").transform.position.x = 0;
transform.position.x = 0;
transform.position.y = 0;
hasreset = true;
}
function takescore ()
{
yield WaitForSeconds (1.0);
playerScore --;
}
function addscore ()
{
yield WaitForSeconds (1.0);
playerScore ++;
}
function OnCollisionExit(collision : Collision) {
if (collision.collider.tag == "paddle") {
rigidbody.velocity.x += collision.gameObject.GetComponent(Paddle2).velocity * frictionTransfer;
}
}
function Update () {
//checking bounds for score and reset pos of ball and paddle
if(transform.position.y <-5.67)
{
resetball();
if(hasreset == true)
{
takescore();
}
hasreset = false;
}
if(transform.position.y >5.67)
{
resetball();
if(hasreset == true)
{
addscore();
}
hasreset = false;
}
}
And the calculation of the velocity in this paddle code:
#pragma strict
//Variable to hold paddle speed
var speed:int = 10;
private var lastPosX : float;
public var velocity = 0.0f;
function Update ()
{
if(Input.GetKey(KeyCode.Escape)){
Application.LoadLevel (0);
}
//player input
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
}
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
}
if(transform.position.x <-7.88)
{
transform.position.x = -7.88;
}
if(transform.position.x >7.88)
{
transform.position.x = 7.88;
}
velocity = (transform.position.x - lastPosX) / Time.deltaTime;
lastPosX = transform.position.x;
}
Note the OnCollisionExit() code requires the paddles have the 'paddle' tag set for both.
That smoothing code was borrowed and also those fragments of weird code was me just trying stuff out :P. Ill give your changes a try thanks!
I am getting an unknown identifier error on the oncollision exit part BCE0005: $$anonymous$$ identifier: 'Paddle2'.
On line 41 above, you need to replace "Paddle2" with whatever you've name your script that handles the paddle.