Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 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 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?

@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.

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

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


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