My ball gameobject in this game will sometimes meet a portal and teleport to a different location. The problem I am running into here is that the trail renderer will draw across the screen to the location the ball is being sent to. I would like the trail renderer to pause for a fraction of a second to make this look visually more appealing than it scribbling a line to the new location.
using UnityEngine;
public class Ball : MonoBehaviour
{
[SerializeField] float pushX = 0.1f;
[SerializeField] float pushY = 10f;
[SerializeField] float randomFactor = 0.2f;
[SerializeField] float LowRandomFactor = -0.2f;
// cached component references
Rigidbody2D rb;
public bool hasStarted = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
int NumberOfBallsLeft = FindObjectsOfType<Ball>().Length;
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnClick();
}
if(gameObject.name == "Ball(Clone)" && NumberOfBallsLeft > 1)
{
hasStarted = true;
}
}
public void LaunchOnClick()
{
if (Input.GetKeyDown("space"))
{
hasStarted = true;
GetComponent<Rigidbody2D>().velocity = new Vector2(pushX, pushY);
}
}
public void LockBallToPaddle()
{
RespawnPos RespawnPoint = FindObjectOfType<RespawnPos>();
Vector2 paddlePos = new Vector2(RespawnPoint.transform.position.x, RespawnPoint.transform.position.y);
transform.position = paddlePos;
}
private void OnCollisionEnter2D(Collision2D collision)
{
Vector2 velocityAdjustment = new Vector2
//X axis
(Random.Range(LowRandomFactor, randomFactor),
//Y axis
Random.Range(0, randomFactor));
if (hasStarted)
{
rb.velocity += velocityAdjustment;
}
else if (collision.transform.CompareTag("DeathBarrier"))
{
hasStarted = false;
}
}
public void ResetPosition()
{
RespawnPos RespawnPoint = FindObjectOfType<RespawnPos>();
hasStarted = false;
Vector2 paddlePos = new Vector2(RespawnPoint.transform.position.x, RespawnPoint.transform.position.y);
transform.position = paddlePos;
}
}
Within my script above I have done this successfully in two places, but cannot replicate it within this instance. It will happen upon the player losing a life because my bool for starting the game is set to false, which I’ve set to program the trail time to 0f. It also happens when the ball is attached to the paddle since this means that the bool is also set to false here.
I’ve tried disabling the trail renderer the same way by referencing to a collision with the portal, but have had no success there. Does anyone have an idea of how I could make the collision with my portal object briefly disable the trail renderer until after the transform of position is complete?
EDIT: I’ve tried to separate the Trail renderer into its own object and create a script purposed with managing the trail. I have updated the above script to reflect changes and am posting the trail managing one below. I am still only having the problem in this one area.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrailManage : MonoBehaviour
{
TrailRenderer tr;
void Start()
{
this.transform.parent.GetComponent<Ball>().hasStarted = false;
tr = GetComponent<TrailRenderer>();
}
// Update is called once per frame
void Update()
{
if (this.transform.parent.GetComponent<Ball>().hasStarted == false)
{
tr.Clear();
Debug.Log("No Trail!");
}
else if (this.transform.parent.GetComponent<Ball>().hasStarted == true)
{
Debug.Log("Trail!");
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
tr.Clear();
}
}