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 /
avatar image
0
Question by shotttt · Jun 08, 2017 at 03:59 PM · unity 2dline renderer

How to shrink a line renderer

I'm making a 2D fishing game, the physics of grabbing are working fine, I only need to display a line (Line Renderer) showing the distance between hook and fish to make it kinda real, and I need this line to shrink while the fish is moving. The problem is, when I set positions in script, the line get drew and stays and the first distance between fish and hook, while the fish moves.

How do I make the setposition function work every frame ? it is inside an "if" clause, so it doesn't really get updated every frame. I hope you guys understand what I'm trying to say. PS: I'm using Unity 5.6

Any help or suggestions ?

Comment
Add comment · Show 3
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 neosca · Jun 08, 2017 at 06:41 PM 0
Share

You need to put the mentioned script here, so we can try to track the issue and help you.

avatar image shotttt neosca · Jun 12, 2017 at 08:05 AM 0
Share

Sorry I forgot that, here's my script: void Update () {

         if(grabbed)
             joint.distance -= step*Time.deltaTime;
 
         RaycastHit2D Hit = new RaycastHit2D();
 
         if(Input.Get$$anonymous$$ouseButton(0) ) {
             TargetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             Vector2 TargetPos2D = new Vector2(TargetPos.x, TargetPos.y); 
             Vector2 CurrentPos2D = new Vector2(transform.position.x, transform.position.y);
 
             Hit = Physics2D.Raycast(CurrentPos2D, TargetPos2D - CurrentPos2D, maxdistance);
             Debug.DrawLine(CurrentPos2D, CurrentPos2D + (TargetPos2D - CurrentPos2D).normalized * maxdistance, Color.black);
 
 
             if(Hit != null){
                 Debug.Log("trueeeeeeeeeee");
                 Debug.Log(Hit.collider);
                 Debug.Log(Hit.point);
                 Debug.Log(joint.anchor);
                 }
 
             if(Hit.collider !=null && Hit.collider.GetComponent<Rigidbody2D>() !=null){
                 grabbed = true;
                 joint.enabled = true;
                 Debug.Log("true" + Hit.collider.gameObject.name);
                 joint.connectedBody = Hit.collider.gameObject.GetComponent<Rigidbody2D>();
                 joint.connectedAnchor = (Hit.point - new Vector2(Hit.collider.transform.position.x, Hit.collider.transform.position.y)).normalized;
                 joint.distance = Vector2.Distance(transform.position, Hit.point);
                 Hit.collider.GetComponent<Rigidbody2D>().gravityScale = 1.2f;
                 Hit.collider.GetComponent<Rigidbody2D>().AddForce(joint.anchor * Speed);
 
                 Line.enabled = true;
             }
                 
     
         }
                 Line.SetPosition(0, new Vector3(this.transform.position.x, this.transform.position.y, -1));
                 Line.SetPosition(1, new Vector3(Hit.collider.transform.position.x, Hit.collider.transform.position.y, -1));
     }
 }
avatar image shotttt · Jun 09, 2017 at 06:43 AM 0
Share

I forgot the code, sorry : btw I changed position of "Line.SetPosition" so it may be updated every frame, but the line keep it's position void Update () {

      if(grabbed)
          joint.distance -= step*Time.deltaTime;
 
      RaycastHit2D Hit = new RaycastHit2D();
 
      if(Input.Get$$anonymous$$ouseButton(0) ) {
          TargetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
          Vector2 TargetPos2D = new Vector2(TargetPos.x, TargetPos.y); 
          Vector2 CurrentPos2D = new Vector2(transform.position.x, transform.position.y);
 
          Hit = Physics2D.Raycast(CurrentPos2D, TargetPos2D - CurrentPos2D, maxdistance);
          Debug.DrawLine(CurrentPos2D, CurrentPos2D + (TargetPos2D - CurrentPos2D).normalized * maxdistance, Color.black);
 
 
          if(Hit != null){
              Debug.Log("trueeeeeeeeeee");
              Debug.Log(Hit.collider);
              Debug.Log(Hit.point);
              Debug.Log(joint.anchor);
              }
 
          if(Hit.collider !=null && Hit.collider.GetComponent<Rigidbody2D>() !=null){
              grabbed = true;
              joint.enabled = true;
              Debug.Log("true" + Hit.collider.gameObject.name);
              joint.connectedBody = Hit.collider.gameObject.GetComponent<Rigidbody2D>();
              joint.connectedAnchor = (Hit.point - new Vector2(Hit.collider.transform.position.x, Hit.collider.transform.position.y)).normalized;
              joint.distance = Vector2.Distance(transform.position, Hit.point);
              Hit.collider.GetComponent<Rigidbody2D>().gravityScale = 1.2f;
              Hit.collider.GetComponent<Rigidbody2D>().AddForce(joint.anchor * Speed);
 
              Line.enabled = true;
          }
              
  
      }
              Line.SetPosition(0, new Vector3(this.transform.position.x, this.transform.position.y, -1));
              Line.SetPosition(1, new Vector3(Hit.collider.transform.position.x, Hit.collider.transform.position.y, -1));
  }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by kornstar83 · Jun 09, 2017 at 01:44 AM

Save the original fish position when the line is drawn and inside an Update function call your function

 public Transform fish;//assign in inspector
 Vector3 lastFishPos;// have this update just before the fish moves
 
 void Update(){
     if (fish.position != lastFishPos) {
         //Call your setposition function here
     }
 }

this way your function won't be called every frame but only when the fish moves.

If you want it to update every frame then just remove the if statement,

 void Update(){
     //Call your setposition function here
 }
Comment
Add comment · Show 2 · 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 shotttt · Jun 09, 2017 at 07:02 PM 1
Share

Thank you very much for the help, I didn't really do exactly what you said but you helped me a lot through the idea of creating " Transform fish", here's what I did. I created a private fish variable that stores the Hit.collider variavble.

 private Transform fish;

then:

 fish = Hit.collider.transform;
 
                 Line.enabled = true;
             }
                 
     
         }
                 Line.SetPosition(0, new Vector3(this.transform.position.x, this.transform.position.y, -1));
                 Line.SetPosition(1, new Vector3(fish.position.x, fish.position.y, -1));
     }


avatar image kornstar83 shotttt · Jun 09, 2017 at 07:44 PM 1
Share

Im happy you found a way to make it work, it was a bit of a shot in the dark on my end because I havent used line renderers yet.

I also realized after posting the answer that you couldn't actually assign the fish transform in the inspector, my mistake.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Drawing 2d line for different resolutions in unity 0 Answers

How to make smoother lines - LineRenderer 1 Answer

customize line renderer from middle 0 Answers

Collision of linerenderer unity2d 1 Answer

Creating multiple lines using multiple Line Renders, but all are receiving the same update. 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