- Home /
Glitching Line Renderers upon tying to new game object
I'm trying to make a 3D ball catapulting type game where balls are launched by swiping backwards on the touch screen. Upon the first ball being thrown the line renderers end point are set to the ball's position as it should be, however after that one is thrown and a new is spawned the line renderers end positions keep seeming to rapidly change between the new ball's position and other points on the screen, resulting in a glitchy inaccurate rope.
I have one script attached to the ball prefab that keeps getting spawned after the first is thrown here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwipeDetect : MonoBehaviour
{
private bool swiping = false;
private float length = 0;
private Vector3 startPos;
private Vector3 endPos;
private Vector3 final;
public float ballHeight = 1.893F;
private Vector3 ballStart;
public GameObject catapultLeft;
public GameObject catapultRight;
private SpringJoint leftJoint;
private SpringJoint rightJoint;
public bool newBall = false;
// Start is called before the first frame update
public void Start()
{
leftJoint = catapultLeft.GetComponent<SpringJoint>();
rightJoint = catapultRight.GetComponent<SpringJoint>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
ballStart = transform.position;
ballStart.y = 0;
final = Vector3.zero;
length = 0;
swiping = false;
Vector2 touchPos = Input.GetTouch(0).position;
startPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
}
if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary))
{
swiping = true;
Vector2 touchPos = Input.GetTouch(0).position;
Vector3 tempPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
Vector3 updatePos = (tempPos - startPos);
updatePos.y = ballHeight + (updatePos.z / 2);
//Debug.Log(ballStart + updatePos);
transform.position = ballStart + updatePos;
//Debug.Log(updatePos);
}
if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Canceled))
{
swiping = false;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && swiping)
{
Vector2 touchPos = Input.GetTouch(0).position;
endPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
final = endPos - startPos;
final *= -1; //reverse vector for fling effect
length = final.magnitude;
foreach (Component c in GetComponents<SpringJoint>())
{
Destroy(c);
}
catapultLeft.GetComponent<LineRenderer>().enabled = false;
catapultRight.GetComponent<LineRenderer>().enabled = false;
newBall = true;
}
}
}
And I also have another attached to an empty gameobject that handles the respawning of the balls:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallHandler : MonoBehaviour
{
public GameObject catapultLeft;
public GameObject catapultRight;
public GameObject ballPrefab;
private Vector3 ballSpawnPos;
private int ballCounter = 0;
// Start is called before the first frame update
void Start()
{
ballSpawnPos = GameObject.Find("Sphere").transform.position;
}
// Update is called once per frame
void Update()
{
if (GameObject.Find("Sphere").GetComponent<SwipeDetect>().newBall)
{
GameObject.Find("Sphere").name = "thrown" + ballCounter.ToString();
GameObject newBall = Instantiate(ballPrefab, ballSpawnPos, Quaternion.identity);
newBall.name = "Sphere";
newBall.GetComponent<SwipeDetect>().catapultLeft = catapultLeft;
newBall.GetComponent<SwipeDetect>().catapultRight = catapultRight;
catapultLeft.GetComponent<LineRenderer>().enabled = true;
catapultRight.GetComponent<LineRenderer>().enabled = true;
Destroy(GameObject.Find("thrown" + ballCounter.ToString()).GetComponent<SwipeDetect>());
ballCounter++;
}
}
}
I also have scripts on the gameobjects with the linerenderers (catapultLeft and catapultRight) that update their position like this:
public class DrawLine1 : MonoBehaviour
{
private LineRenderer lr;
public Material colour;
// Start is called before the first frame update
public void Start()
{
lr = GetComponent<LineRenderer>();
lr.enabled = false;
lr.startWidth = 0.02F;
lr.endWidth = 0.02F;
lr.positionCount = 2;
lr.SetPosition(0, new Vector3(GameObject.Find("Sphere").transform.position.x - 0.5F, GameObject.Find("Sphere").transform.position.y - 0.12F, GameObject.Find("Sphere").transform.position.z));
lr.material = colour;
lr.enabled = true;
}
// Update is called once per frame
void Update()
{
lr.SetPosition(1, GameObject.Find("Sphere").transform.position);
}
}
I really can't seem to tell what this issue might be because I never really seemed to notice this issue before, this issue only occurs after the first throw though, the renderer's 1 position is always correct until this point. Does anyone have any idea what could be the case? Thanks.
Answer by Batzuga · Feb 07, 2020 at 10:11 PM
If you have multiple balls in the air called Sphere. And your script is just GameObject.Find("Sphere") it won't know which sphere to look for, so it keeps alternating between the two. So the line renderer is trying to draw line to both of the spheres.
Hi, I thought that could be an issue before so these lines were added to my Handler script a while back upon the spawning sequence:
GameObject.Find("Sphere").name = "thrown" + ballCounter.ToString();
GameObject newBall = Instantiate(ballPrefab, ballSpawnPos, Quaternion.identity);
newBall.name = "Sphere";
Is this not the right way to be doing it or could something else be the case?
Your answer
Follow this Question
Related Questions
Line Renderers tied to ball keep teleporting/glitching after instantiate 1 Answer
I have watched Brackeys Saving system video but its not working 1 Answer
Mobile Build and Playmode Terrain Collision Disparity 0 Answers
How to control 2 axes of object in 3d world 0 Answers
How do I change the pivot points on each poly of this icosphere? 2 Answers