Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LetalisDev · Feb 07, 2020 at 08:51 PM · 3dmobileinstantiate prefabline renderer

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LetalisDev · Feb 08, 2020 at 11:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Line Renderers tied to ball keep teleporting/glitching after instantiate 1 Answer

How to optimize 3D game on mobile? 0 Answers

Best approach for a gun powder trail? 0 Answers

my character movement act weirdly when i activate my camera follow script 1 Answer

Mobile joystick moving my player like the game is played in portait screen, but the game is in landscape mode. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges