i am making a pong game and it works fine till i reset the game
using UnityEngine; using System.Collections;
public class Ball : MonoBehaviour { public Rigidbody2D RigidBall; private AudioSource snd;
// Use this for initialization
void Start ()
{
Collider collider = GetComponent<Collider>();
RigidBall = GetComponent<Rigidbody2D>();
snd = GetComponent<AudioSource>();
Invoke("ballThrow", 3.0f);
}
public void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.name == "Player01" || coll.gameObject.name=="Player02")
{
snd.Play();
}
}
public void ResetBall()
{
RigidBall.angularVelocity = 0;
RigidBall.transform.position = new Vector3(0, 0, 0);
RigidBall.velocity = Vector3.zero;
RigidBall.isKinematic = false;
Invoke("ballThrow", 2.0f);
}
public void ballThrow()
{
float randomvar = Random.Range(0, 2);
if (randomvar <= 0.5)
{
RigidBall.AddForce(new Vector2(50, -10));
}
else
{
RigidBall.AddForce(new Vector2(-50, -10));
}
}
}
Comment
Answer by Cryptosan · Oct 02, 2016 at 01:31 PM
Wait, I can't understand clearly. Is it this?: You reset the pong game but it does not work? If not, reply to me for what you are clearly trying to say.
Sorry.
on resetting (after the game ends), it resets position but all of a sudden jumps up and down with a high velocity.But if i quickly press the reset again it works fine until the game ends and the same problem continues