- Home /
The question is answered, right answer was accepted
Line renderer location problem
Im having a problem using Line Renderers on one of my unit gameobjects. When using World space it fires correctly at an enemy gameobject but the line renderer is at an odd position in the world (Picture unitusingworldspace). Without using world space the Line renderer is in the correct location at the tip of the gun (Picture unitnotusingworldspace) but when playing the game the line renderer is not showing up in the correct place at the tip of the gun anymore. How can I fix this?
I only see a red "something", what am I supposed to see. The LineRenderer Component is turned off ins$$anonymous$$d of on. To what does the position of the gizmo correspond? The difference in position is weird considering the same position values in the inspector. How is the gizmo configured? Center/ Pivot, Global/ Local?
Ah, by changing the Gizmo configuration I have removed the weird offset by changing the gizmo center option to pivot, local. Now the unit still uses worldspace but the offset is over the unit. Im not sure if I will run into problems when using worldspace on the line renderer though. Thank you for mentioning the gizmo configuration, I would have never noticed that @hexagonis
Did you change Position element 0 and 1 in script?
Yes inside a script the position of the gun is used as one location and the other position is the enemy Gameobject
The code is as follows for the position setup of the line renderer
Lazer.SetPosition(0, gun.transform.position);
Lazer.SerPosition(1, enemy.transform.position);
I think when you use World space of LineRender. It will use Transform position. Which what it actually use is localTransform since LineRenderer is child of Gun.
I think you have to inverse Transform from local -> world space. Try it and see if it work
This seemed to work thank you. Without having the "World space" box selected use the code below to allow the line renderer to shoot correctly:
Vector3 GunPos = transform.InverseTransformPoint(Gun.transform.position);
Lazer.SetPosition (0, GunPos);
Vector3 EnemyPos = transform.InverseTransformPoint(chaseTarget.transform.position);
Lazer.SetPosition (1, EnemyPos);
Follow this Question
Related Questions
How to move two same Objects at the same place in different scenes? 1 Answer
How to set a sprite's location to another sprite based on RNG. 1 Answer
Convert terrain map coordinates (GetDetailLayer) into world position 0 Answers
transform.position is giving me local space 1 Answer
Is there an end to Unity world space? 3 Answers