- Home /
Ball disappears after invoking a method
Hi! I'm trying to make a Pong game as a "challenge", it pretty much works, except I'm having problems when a player scores a point.
The following should happen:
Game starts, ball in 0,0 with zero velocity
Wait 3 seconds
Invoking the SetVelocity method, which sets a starting velocity for the ball
Ball bouncing of the top and bottom walls, and the paddles
A player scores a point
Calls for the Start() method so:
Set the ball's velocity to 0
Set the ball's position to 0,0
Wait 3 seconds
Invoking the SetVelocity method, which sets...
And this goes on..
But, when after the ball position is set after when a player scores a point, the ball gets to center, it stays there for a little but definitely a lot less than 3 seconds and disappears. And I have no idea why. Any suggestions?
Oh and by the way here's the ball's code: (also tried if not calling the start method, but doing the same, no difference)
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
public float speed;
public ScoreHandler scorehandler;
public float delay = 3.0f;
void Start ()
{
rigidbody2D.velocity = new Vector2 (0f, 0f);
//Debug.Log ("Setted velocity to 0");
transform.position = new Vector3 (0.0f, 0.0f, 0.0f);
//Debug.Log ("Setted position to the middle");
Invoke ("SetVelocity", delay);
//Debug.Log ("Invoked SetVelocityMethod");
}
void Update(){
//Debug.Log (rigidbody2D.velocity);
}
void SetVelocity (){
//Debug.Log ("Setting starting velocity");
rigidbody2D.velocity = Vector2.one.normalized * speed;
//Debug.Log ("Setted starting velocity");
}
float hitFactor (Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
return (ballPos.y - racketPos.y) / racketHeight;
}
void OnCollisionEnter2D (Collision2D col)
{
//Debug.Log (col.gameObject.name);
if (col.gameObject.name == "paddleLeft")
{
//Debug.Log (col.gameObject.name);
float y = hitFactor (transform.position,
col.transform.position,
((BoxCollider2D)col.collider).size.y);
Vector2 dir = new Vector2 (1, y).normalized;
rigidbody2D.velocity = dir * speed;
} else if (col.gameObject.name == "paddleRight")
{
float y = hitFactor (transform.position,
col.transform.position,
((BoxCollider2D)col.collider).size.y);
Vector2 dir = new Vector2 (-1, y).normalized;
rigidbody2D.velocity = dir * speed;
} else if (col.gameObject.name == "wallRight")
{
scorehandler.AddLeft ();
/*rigidbody2D.velocity = new Vector2(0f,0f);
transform.position = new Vector3 (0.0f, 0.0f);
Invoke ("SetVelocity", delay);*/
Start();
} else if (col.gameObject.name == "wallLeft")
{
scorehandler.AddRight ();
/*rigidbody2D.velocity = new Vector2(0f,0f);
transform.position = new Vector3 (0.0f, 0.0f);
Invoke ("SetVelocity", delay);*/
Start();
}
}
}
You should not Call Start() in different parts of your code, create another function for example Init() and call it in Start() and wherever you want to reset your parameters. I don't know if that's the source of your problem but it would be safer to do like this :p .
Yeah, I thought it may be a problem, so I basicly copied exactly what the Start() does, but as I said, no difference, but thanks it was still helpful. :D
Problem solved, actually the Ball was destroyed by an auto-destroy system which was the TrailRenderer!
Your answer
Follow this Question
Related Questions
Ball stuck horizontally between rackets in pong 0 Answers
Prevent ball from going straight on x and y axis 2 Answers
What is the best way to move a paddle accurately in a circle? 2 Answers
A ball drops down, how to reverse the velocity? 2 Answers
Why my ball is slowing after contact with other object? 1 Answer