- Home /
This question was
closed Jul 31, 2020 at 03:25 PM by
mjking21.
i am not able to increase the speed of ball perodically in pong, in this code the speed of ball decrease while playing so help with this, how to increase speed of ball perodically?,
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BallControl : MonoBehaviour {
private Rigidbody2D rb2d;
private Vector2 vel;
void GoBall() {
float rand = Random.Range (0, 2);
if (rand < 1) {
rb2d.AddForce (new Vector2 (20, -15));
} else {
rb2d.AddForce (new Vector2 (-20, -15));
}
}
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
Invoke ("GoBall", 2);
}
void ResetBall() {
vel = new Vector2 (0, 0);
rb2d.velocity = vel;
transform.position = Vector2.zero;
}
void RestartGame() {
ResetBall ();
Invoke ("GoBall", 1);
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.collider.CompareTag ("Player")) {
vel.x = rb2d.velocity.x;
vel.y = (rb2d.velocity.y / 2.0f) + (coll.collider.attachedRigidbody.velocity.y / 3.0f);
rb2d.velocity = vel;
}
}
}
Comment
Answer by eneIr · Jul 23, 2020 at 10:08 AM
Since your English is pretty bad, I guess you want to increase the speed of the ball periodically, right? If so, you can change your OnCollisionEnter2D to this:
float speedIncrement;
vel.x = rb2d.velocity.x + speedIncrement;
vel.y = (rb2d.velocity.y / 2f) + coll.collider.attachedRigidbody.velocity.y / 3f + speedIncrement;
rb2d.velocity = vel;
Hope it works!