- Home /
Line renderer renders a line at previous position for a short while before correctly spawning at the correct position.
Hello, I'm having two issues with my 2D project and I will outline both of them below.
Firstly, I have a Player
prefab which I'm instantiating using an Object Pooler script. The Player
prefab has two children, a Circle
prefab that will move around via Physics and a Catapult
that contains a LineRenderer which propels the Player
.
The Player
is spawned at a random position via a GameController
script, stays active in the scene for a while, gets deactivated and spawned again after a while at another position.
The Circle
is spawned in the correct position every time, however the Catapult
will display a Line
from the Player
's previous position to the Player
's correct position for a split second before spawning it in the correct position.
This is the GameController
script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public static GameController SharedInstance;
private float startPlayerWait = 2.0f;
private float spawnPlayerInterval = 5.0f;
private void Awake()
{
SharedInstance = this;
}
private void Start()
{
SetupBoundaries();
StartCoroutine(SpawnPlayer());
}
IEnumerator SpawnPlayer()
{
yield return new WaitForSeconds(startPlayerWait);
while (true)
{
float spawnY = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
float spawnX = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
Vector3 spawnPosition = new Vector3(spawnX, spawnY, 0);
GameObject player = ObjectPooler.SharedInstance.GetPooledObject("Player");
if (player != null)
{
player.transform.position = spawnPosition;
player.SetActive(true);
player.transform.Find("Circle").gameObject.SetActive(true);
player.transform.Find("Catapult").gameObject.SetActive(true);
player.GetComponentInChildren<ProjectileDragging>().enabled = true;
player.GetComponentInChildren<ProjectileDragging>().CustomOnEnable(spawnPosition);
}
yield return new WaitForSeconds(spawnPlayerInterval);
}
}
}
As you can see in the SpawnPlayer
coroutine, I'm setting the Player
's position to the new random position before activating the Player
object and its children.
This is the ProjectileDragging
script which handles the rendering of the line and the launching of the projectile.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileDragging : MonoBehaviour
{
public float maxStretch = 3.0f;
public LineRenderer catapultOne;
private SpringJoint2D spring;
private Transform catapult;
private Ray rayToMouse;
private Ray catapultOneToProjectile;
private float maxStretchSqr;
private float circleRadius;
private bool clickedOn;
private Vector2 prevVelocity;
public void CustomOnEnable(Vector3 spawnPosition)
{
spring = GetComponent<SpringJoint2D>();
catapult = spring.connectedBody.transform;
Debug.Log("Custom enable: " + spring.connectedBody.transform.position);
catapult.position = spawnPosition;
LineRendererSetup();
rayToMouse = new Ray(catapult.position, Vector3.zero);
catapultOneToProjectile = new Ray(catapultOne.transform.position, Vector3.zero);
maxStretchSqr = maxStretch * maxStretch;
CircleCollider2D circle = GetComponent<CircleCollider2D>();// collider2D as CircleCollider2D;
circleRadius = circle.radius;
}
void FixedUpdate()
{
if (clickedOn)
Dragging();
if (spring != null)
{
if (!GetComponent<Rigidbody2D>().isKinematic && prevVelocity.sqrMagnitude > GetComponent<Rigidbody2D>().velocity.sqrMagnitude)
{
Destroy(spring);
GetComponent<Rigidbody2D>().velocity = prevVelocity;
}
if (!clickedOn)
prevVelocity = GetComponent<Rigidbody2D>().velocity;
LineRendererUpdate();
}
else
{
catapultOne.enabled = false;
}
}
void LineRendererSetup()
{
catapultOne.SetPosition(0, catapultOne.transform.position);
}
void LineRendererUpdate()
{
Vector2 catapultToProjectile = transform.position - catapultOne.transform.position;
catapultOneToProjectile.direction = catapultToProjectile;
Vector3 holdPoint = catapultOneToProjectile.GetPoint(catapultToProjectile.magnitude + circleRadius);
catapultOne.SetPosition(1, holdPoint);
}
void Dragging()
{
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if (catapultToMouse.sqrMagnitude > maxStretchSqr)
{
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint(maxStretch);
}
mouseWorldPoint.z = 0f;
transform.position = mouseWorldPoint;
}
private void OnMouseDown()
{
spring.enabled = false;
clickedOn = true;
}
private void OnMouseUp()
{
spring.enabled = true;
GetComponent<Rigidbody2D>().isKinematic = false;
clickedOn = false;
this.gameObject.GetComponent<Ball>().setCurrenState(Ball.PlayerState.MOVING);
}
}
This script is a component under the Circle
prefab and is disabled, therefore it won't be called when the object is activated.
The line player.GetComponentInChildren<ProjectileDragging>().enabled = true;
activates the script and the line renderer is set up in the method LineRendereSetup(). My question is why is it rendering the line for a split second from the position
(0, 0, 0)` to its new position and keeps doing that each time its activated.
My second question: when dragging the circle, if its get deactivated and activated again, why does it still follow the mouse? Shouldn't it remain fixed in its new position until I drag it again?
Your answer
Follow this Question
Related Questions
Particle effects along line renderer? 0 Answers
Achieve a "stroke" / "transparent" effect with the Line Renderer in 2D 0 Answers
What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer
How can I increase the view-distance of a trail renderer? 1 Answer
Neon / Glow effect on lines in 2d 1 Answer