Have you ever wandered how to animate aiming in your game? Check out this quick tutorial.
Aiming with LineRenderer in Unity is very simple. You can use it as handy targeting element in Your game!
Steps to make it work:
1. Create new GameObject;
2. Add new GameObject as a child of previous GameObject;
3. Add LineRenderer element In child;
We attach LineRenderer to child objects instead of parent because each GameObject should have only one Renderer komponent. The same „trick” could be used if we want to have multiple colliders within GameObject – Just add child object with collider.
4. Then we could obtain reference to LineRenderer – if we are using only one additional Renderer, this piece of code will get the work done:
linRend = gameObject.GetComponentInChildren();
5. Next we will add something like this somewhere in game’s code:
linRend.SetVertexCount(2); //Line made from two points
linRend.SetPosition(0, rigid.position); //set position of first point
linRend.SetPosition(1, getGamePoint(Input.mousePosition)); //set position of second point
linRend.SetWidth(1f, 0f); //Line will change their width from 1f to 0f, creating a triangle.
6. Thats it!
Some screenshots how it may look like in your game:
Leave a comment below if you have any questions 🙂
Aliasing Games Team