• 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 blackroomboy · Jan 01, 2020 at 10:52 AM · linerendererline rendererdraggingawakeenable and disable script

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 w$$anonymous$$ch I'm instantiating using an Object Pooler script. The Player prefab has two c$$anonymous$$ldren, a Circle prefab that will move around via Physics and a Catapult that contains a LineRenderer w$$anonymous$$ch propels the Player.

The Player is spawned at a random position via a GameController script, stays active in the scene for a w$$anonymous$$le, gets deactivated and spawned again after a w$$anonymous$$le 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.

T$$anonymous$$s 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 = t$$anonymous$$s;
     }
 
     private void Start()
     {
         SetupBoundaries();
         StartCoroutine(SpawnPlayer());
     }
 
     IEnumerator SpawnPlayer()
     {
         yield return new WaitForSeconds(startPlayerWait);
 
         w$$anonymous$$le (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.GetComponentInC$$anonymous$$ldren<ProjectileDragging>().enabled = true;
                 player.GetComponentInC$$anonymous$$ldren<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 c$$anonymous$$ldren.

T$$anonymous$$s is the ProjectileDragging script w$$anonymous$$ch handles the rendering of the line and the launc$$anonymous$$ng 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;
         t$$anonymous$$s.gameObject.GetComponent<Ball>().setCurrenState(Ball.PlayerState.MOVING);
     }
 }
 

T$$anonymous$$s 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.GetComponentInC$$anonymous$$ldren<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?

@Cherno @Jessy @sj631 @Eric5h5 @Bunny83

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

118 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

Related Questions

Issue drawing lines between buttons - Lines overlap the buttons 2 Answers

LineRenderer acts strangely 0 Answers

Line render shows only vertexes when using unity recorder 0 Answers

How to delete the first point of a line renderer's array until there are only an specific amount of points. 1 Answer

Draw out Line in C# 0 Answers


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