- Home /
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 ?
You need to put the mentioned script here, so we can try to track the issue and help you.
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));
}
}
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));
}
}
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
}
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));
}
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
Follow this Question
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