I’m using pixelmaker’s Surge script for a game in which balls spawn on the left side of the screen and move to the right, bouncing on a paddle that the player moves. If the player misses the ball, it falls below the paddle and the player loses health.
This is my first personal project and I’m very happy with how much I’ve learned and how many bugs I’ve managed to fix so far, but this one has got me completely stumped:
Sometimes the ball will teleport briefly, either to the beginning of its previous spline, or to the beginning of its next spline before it has moved. So if the paddle is in position 1, the ball will bounce over to position 2, then teleport to position 1 for a fraction of a second and detect the collision with the paddle that has not moved, then bounce over to position 3.
I’ve already started to think about how I could completely rewrite this script to possibly fix this, but if anybody has any ideas, I would much rather fix what I already have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pixelplacement;
public class BallMovement : MonoBehaviour
{
private GameManager gameManager;
private GameObject player;
public Transform ball;
private Spline startSpline;
private Spline highSpline;
private Spline medSpline;
private Spline lowSpline;
private Spline fallSpline;
private Spline endSpline;
public AnimationCurve bounceCurve;
public AnimationCurve startBounceCurve;
public AnimationCurve endBounceCurve;
// Animation durations (playback speed)
public float firstDuration = 2.5f;
public float highDuration = 3f;
public float medDuration = 2.5f;
public float lowDuration = 2f;
public float fallDuration = 0.4f;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
player = GameObject.Find("Player");
// Get the splines (paths) for all bounce types
ball = transform.GetChild(0);
startSpline = transform.GetChild(1).GetChild(0).GetComponent<Spline>();
highSpline = transform.GetChild(1).GetChild(1).GetComponent<Spline>();
medSpline = transform.GetChild(1).GetChild(2).GetComponent<Spline>();
lowSpline = transform.GetChild(1).GetChild(3).GetComponent<Spline>();
fallSpline = transform.GetChild(1).GetChild(4).GetComponent<Spline>();
endSpline = transform.GetChild(1).GetChild(5).GetComponent<Spline>();
startSpline.enabled = true;
// Start first bounce
StartCoroutine(FirstBounce());
}
private void OnTriggerEnter(Collider other)
{
// Only if colliding with player, not other balls
if (other.CompareTag("Player"))
{
// Stop ball from continuing to fall
StopAllCoroutines();
// Unused splines are disabled to prevent teleportation (this didn't fix it)
startSpline.enabled = false;
medSpline.enabled = false;
lowSpline.enabled = false;
highSpline.enabled = false;
fallSpline.enabled = false;
endSpline.enabled = false;
// Move splines right to next position
int moveRight = 8;
startSpline.transform.Translate(new Vector3(moveRight, 0, 0));
lowSpline.transform.Translate(new Vector3(moveRight, 0, 0));
medSpline.transform.Translate(new Vector3(moveRight, 0, 0));
highSpline.transform.Translate(new Vector3(moveRight, 0, 0));
fallSpline.transform.Translate(new Vector3(moveRight, 0, 0));
if (ball.transform.position.x > 10)
{
// Final bounce
StartCoroutine(LastBounce());
}
else
{
// Randomly select next bounce type
int bounceSelect = Random.Range(0, 3);
switch (bounceSelect)
{
case 0:
StartCoroutine(LowBounce());
break;
case 1:
StartCoroutine(MedBounce());
break;
case 2:
StartCoroutine(HighBounce());
break;
}
}
}
// Bounce animations
// Unused splines are disabled to fix bug that lead to ball teleporting for a fraction of a second, leaving weird trails
// and bouncing when they should be missed.
// but turns out that didn't fix it idk
IEnumerator FirstBounce()
{
Tween.Spline(startSpline, ball, 0.5f, 1, false, firstDuration, 0, startBounceCurve, Tween.LoopType.None);
Tween.Spline(fallSpline, ball, 0, 1, false, fallDuration, firstDuration, Tween.EaseLinear, Tween.LoopType.None);
yield return null;
}
IEnumerator LastBounce()
{
endSpline.enabled = true;
Tween.Spline(startSpline, ball, 0, 0.6f, false, firstDuration, 0, endBounceCurve, Tween.LoopType.None);
yield return null;
}
IEnumerator HighBounce()
{
highSpline.enabled = true;
Tween.Spline(highSpline, ball, 0, 1, false, highDuration, 0, bounceCurve, Tween.LoopType.None);
Tween.Spline(fallSpline, ball, 0, 1, false, fallDuration, highDuration, Tween.EaseLinear, Tween.LoopType.None);
timeToPaddleHit = highDuration;
yield return null;
}
IEnumerator MedBounce()
{
medSpline.enabled = true;
Tween.Spline(medSpline, ball, 0, 1, false, medDuration, 0, bounceCurve, Tween.LoopType.None);
Tween.Spline(fallSpline, ball, 0, 1, false, fallDuration, medDuration, Tween.EaseLinear, Tween.LoopType.None);
timeToPaddleHit = medDuration;
yield return null;
}
IEnumerator LowBounce()
{
lowSpline.enabled = true;
Tween.Spline(lowSpline, ball, 0, 1, false, lowDuration, 0, bounceCurve, Tween.LoopType.None);
Tween.Spline(fallSpline, ball, 0, 1, false, fallDuration, lowDuration, Tween.EaseLinear, Tween.LoopType.None);
timeToPaddleHit = lowDuration;
yield return null;
}
}