Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Mar 31, 2019 at 04:18 PM by syfen for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by syfen · Mar 31, 2019 at 04:04 PM · linerendererslingshot

Linerenderer not following game object

So I have done the first video of the angry birds tutorial and I have a problem with the line renderer. The meteor (I am using a ball instead) is supposed to have a band (line renderer) attached to it, but it's not following the ball. I have searched for solutions, but it seems like no one else is having the same problem, maybe because line renderer has been updated (IDK), and I have been searching for errors in the code, but I can't find any mistakes. Help would be appreciated. Here is a picture: alt text Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ProjectileDragging : MonoBehaviour
 {
 
     public float maxStretch = 3.0f;
     public LineRenderer catapultLineFront;
     public LineRenderer catapultLineBack;
 
     private Rigidbody2D Ball;
     private SpringJoint2D spring;
     private Transform catapult;
     private Ray rayToMouse;
     private Ray leftCatapultToProjectile;
     private float maxStretchSqr;
     private float circleRadius;
     private bool clickedOn;
     private Vector2 prevVelocity;
 
     private void Awake()
     {
         spring = GetComponent<SpringJoint2D>();
         catapult = spring.connectedBody.transform;
 
         Ball = GetComponent<Rigidbody2D>();
     }
 
     void Start()
     {
         maxStretchSqr = maxStretch * maxStretch;
 
         LineRendererSetup();
         //catapult = GetComponent<Transform>();
         leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
         CircleCollider2D circle = GetComponent<Collider2D>() as CircleCollider2D;
         circleRadius = circle.radius;
         rayToMouse = new Ray(catapult.position, Vector3.zero);
 
     }
 
     void Update()
     {
         if (clickedOn)
         {
             Dragging();
         }
 
         if (spring != null)
         {
             if (!Ball.isKinematic && prevVelocity.sqrMagnitude > Ball.velocity.sqrMagnitude)
             {
                 Destroy(spring);
                 Ball.velocity = prevVelocity;
             }
 
             if (!clickedOn)
             {
                 prevVelocity = Ball.velocity;
             }
         }
         else
         {
             catapultLineFront.enabled = false;
             catapultLineBack.enabled = false;
         }
     }
 
     void LineRendererSetup()
     {
         catapultLineFront.SetPosition(0, catapultLineFront.transform.position);
         catapultLineBack.SetPosition(0, catapultLineBack.transform.position);
 
         catapultLineFront.sortingLayerName = "Foreground";
         catapultLineBack.sortingLayerName = "Foreground";
 
         catapultLineFront.sortingOrder = 3;
         catapultLineBack.sortingOrder = 1;
     }
 
     void OnMouseDown()
     {
         spring.enabled = false;
         clickedOn = true;
     }
 
     private void OnMouseUp()
     {
         spring.enabled = true;
         Ball.isKinematic = false;
         clickedOn = false;
     }
 
     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;
     }
 
     void LineRendererUpdate()
     {
         Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
         leftCatapultToProjectile.direction = catapultToProjectile;
         Vector3 holdPoint = leftCatapultToProjectile.GetPoint(catapultToProjectile.magnitude + circleRadius);
         catapultLineFront.SetPosition(1, holdPoint);
         catapultLineBack.SetPosition(1, holdPoint);
 
     }
 }


screenshot-115.png (21.0 kB)
Comment
Add comment · Show 2
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 RobAnthem · Mar 31, 2019 at 04:11 PM 1
Share

The linerenderer position has to be updated every frame if you want the line to move.

avatar image syfen RobAnthem · Mar 31, 2019 at 04:16 PM 0
Share

Thank you very much, sir, I will read my code more carefully next time-

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

102 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

Related Questions

line renderer help 1 Answer

how change linerenderer with png or material.... 0 Answers

Creating a LineRenderer: its always null 2 Answers

create Gizmos such as a wire sphere or line in Update Function 1 Answer

Trying to draw a box with Line Renderer, trouble 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