Keep LineRenderer from changing width in 2D
So I have seen quite a few issues with LineRenderer but none of them answer my issue and I have been unable to find a source in the forums, manuals, or script examples. So my issue is:
Using LineRenderer for the red sight of a gun in a 2D shooter
Red sight goes from gun (its parent object) to mouse pointer using CameraToWorldPoint
Looking through scene view the beam is always there, but in game view it slims down to nothing based on the position.
The code is simple in my head but it doesn't want to work correctly. Anyone know what causes this or need more information for what I messed up?
mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); snipeTrail.SetPosition(0, transform.position); snipeTrail.SetPosition(1, Camera.main.ScreenToWorldPoint(Input.mousePosition));
Adding an image to show this, scene view different than game view:
Answer by thirdcoke · Jan 26, 2016 at 08:45 AM
I'm not familiar with your scene, but I create an example myself, hope this will help. Create a new scene, create a cube, attach the script on the cube. Then click Play link text
While not directly helping it did create the bridge to help me figure it out. I ran your line on a new scene attached to a cube and was able to get the line rendered in full. Since I was running it in 3d mode I was able to see to rotation of the line which the camera always sees in full from the front. Long story short: I focused on the camera ins$$anonymous$$d of the LineRenderer and found that adding a vector to account for the "screentoworldpoint" being at the camera along the z axis it would distort it the game view. Even shorter: snipeTrail.SetPosition(1, Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10));
with the 10 in the new vector being the camera depth that will be changed to a reference over hardcoding it. Extra tidbit: learned about RequireComponent in scripts Thanks!
While not directly helping it did create the bridge to help me figure it out. I ran your line on a new scene attached to a cube and was able to get the line rendered in full. Since I was running it in 3d mode I was able to see to rotation of the line which the camera always sees in full from the front. Long story short: I focused on the camera ins$$anonymous$$d of the LineRenderer and found that adding a vector to account for the "screentoworldpoint" being at the camera along the z axis it would distort it the game view. Even shorter: snipeTrail.SetPosition(1, Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10));
with the 10 in the new vector being the camera depth that will be changed to a reference over hardcoding it. Extra tidbit: learned about RequireComponent in scripts Thanks!
Your answer
Follow this Question
Related Questions
Linerenderer BUG,Problem with the LineRenderer.. 1 Answer
Drawing a "rope" when the player jumps while not grounded 0 Answers
Fall Through Platform Script? 0 Answers
Proper way to update enemies in a large 2d level. 1 Answer
Make Character Selection 0 Answers