Line Renderer Edgy/Jittery
I'm fairly new to Unity and I'm trying to make a simple 2D platformer. I'm currently trying to implement a trajectory arc using the line renderer, which I have managed to do. Unfortunately, it appears to be edgy and jittery, especially on the end, as I manually set 'position count' depending on whether it intersects with game objects or a camera edge.
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
_isMouseDown = true;
_beginMouseDownTime = Time.time;
}
if (_isMouseDown)
{
float sin = Mathf.Sin(((Time.time - _beginMouseDownTime) * 0.5f) - Mathf.PI * 0.5f);
float power = Mathf.Lerp(0.2f, 1f, (sin + 1) * 0.5f) * VelocityMultiplier;
Vector3 pos = new Vector3(transform.position.x + 0.2f, transform.position.y + 0.2f, 0f);
Vector3 vel = new Vector3((power * 1.2f) / 2f, power, 0f);
_lineRenderer.positionCount = LineRendererVertices;
for(int i = 0; i < LineRendererVertices - 1; i++)
{
if(lineIntersects(pos))
{
_lineRenderer.positionCount = i;
break;
}
_lineRenderer.SetPosition(i, pos);
vel += _gravity * Time.deltaTime;
pos += vel * Time.deltaTime;
}
}
}
private bool lineIntersects(Vector3 pos)
{
if(Camera.main.WorldToScreenPoint(pos).y <= 0f)
{
return true;
}
foreach (GameObject platform in ObjectPooler.instance.GetActivePooledObjectsOfTag("Platform"))
{
if (platform.GetComponent<BoxCollider2D>().bounds.Contains(pos))
{
return true;
}
}
return false;
}
Is there a way I can get it to be smoother?
Your answer
Follow this Question
Related Questions
Jerky, jittering, lagging Movement on iPhone 0 Answers
How to fix minor jitter/stutter when moving a 2D character using physics in FixedUpdate() 0 Answers
Agent rotates when destination is set, causing it to get stuck and not move. 0 Answers
LineRenderer - highlight segment on mouseOver? 0 Answers
How do I add points to a LineRenderer Width curve as I'm "drawing" the line? 0 Answers