how to draw 3d lunging forward at you

Draw a curve using linerenderer

Sometimes, yous demand to draw lines, circles or curves in your Unity games. In these cases, you tin utilize Unity's LineRenderer class. In this tutorial, nosotros volition see how we can draw lines, polygons, circles, wave functions, Bézier Curves. And also we will run across how we can do a free cartoon using Line Renderer in Unity3D. In guild to see our other Unity tutorials, click here.

Line Renderer Component

To describe a line we take to add a LineRenderer component to a game object. Even if this component can be attached to any game object, I suggest yous create an empty game object and add the LineRenderer to this object.

We need a cloth which volition exist assigned to LineRenderer. To do this create a textile for the line in Project Tab. Unlit/Colour shader is suitable for this fabric.

Line Material of Line Renderer in Unity3D

Assign LineMat material to the Line Renderer component.

adding line material

Line Renderer draws lines between determined positions. In other words, nosotros tell the Line Renderer the points which will be connected and Line Renderer connects these points.

Line width of the line which will be rendered

In the Positions department, you can change the number of points and positions of points. If you enter two different points, y'all will get a direct line. You can also alter the width of the line in the section beneath.

A straight line which is rendered using line renderer in Unity3D

Too, two describe a triangle, you need 3 points and to draw a rectangle you need 4 points. Let'south draw a rectangle as an example.

To describe a rectangle, we need to set positions of 4 points. We as well accept to check the Loop toggle to obtain a closed shape.

A square which is rendered using line renderer in Unity3D

Drawing Lines From C# Script

If we want to describe or command lines in real-fourth dimension, we need to create a C# script. To describe lines from a script, nosotros determine the size of position array and coordinates of positions in C# script. Therefore, LineRenderer can connect the points.

Allow'south draw a triangle using a script as an example. Commencement, create a script with the proper name "DrawScript". And adhere this script to a game object which already has a LineRenderer component.

          public          class          DrawScript          :          MonoBehaviour          {          private          LineRenderer lineRenderer;          void          Start(          )          {          lineRenderer          =          GetComponent<LineRenderer>          (          )          ;          Vector3[          ]          positions          =          new          Vector3[          3          ]          {          new          Vector3(          0          ,          0          ,          0          )          ,          new          Vector3(          -          1          ,          ane          ,          0          )          ,          new          Vector3(          one          ,          ane          ,          0          )          }          ;          DrawTriangle(positions)          ;          }          void          DrawTriangle(Vector3[          ]          vertexPositions)          {          lineRenderer.positionCount          =          3          ;          lineRenderer.SetPositions(vertexPositions)          ;          }          }        

This script will describe a triangle. Note that we already set up the line width to 0.1 and checked the loop toggle, before. Therefore the same setting is besides valid here.

A triange which is rendered using line renderer in Unity3D

We can also modify the line width from the script using startWidth and endWidth. In addition to this, if yous would like to change line width past position, y'all can set different values to them. In this case, Line Renderer will interpolate the line width according to position.

          public          form          DrawScript          :          MonoBehaviour          {          private          LineRenderer lineRenderer;          void          First(          )          {          lineRenderer          =          GetComponent<LineRenderer>          (          )          ;          Vector3[          ]          positions          =          new          Vector3[          iii          ]          {          new          Vector3(          0          ,          0          ,          0          )          ,          new          Vector3(          -          1          ,          1          ,          0          )          ,          new          Vector3(          1          ,          i          ,          0          )          }          ;          DrawTriangle(positions,          0.02          f          ,          0.02          f          )          ;          }          void          DrawTriangle(Vector3[          ]          vertexPositions,          float          startWidth,          float          endWidth)          {          lineRenderer.startWidth          =          startWidth;          lineRenderer.endWidth          =          endWidth;          lineRenderer.loop          =          truthful          ;          lineRenderer.positionCount          =          3          ;          lineRenderer.SetPositions(vertexPositions)          ;          }          }        

Drawing Regular Polygons and Circles

In this department, we are going to run into how we can write a method that draws regular polygons. Since circles are northward-gons which has big n, our function will exist useful for circles besides. But kickoff, let me explain the mathematics behind it.

Vertices of regular polygons are on a circle. Also, the center of the circle and the center of the polygon are top of each other. The nigh reliable method to draw a polygon is to discover the angle between successive vertices and locate the vertices on the circle. For case, angle of the arc between successive vertices of a pentagon is 72 degrees or for an octagon, it is 45 degrees. To discover this angle, nosotros can divide 360 degrees(or 2xPI radians) with the number of vertices.

Rotation matrices

Then we demand to discover the positions of the vertices. To exercise this we assign an initial point for the first vertex and rotate this vertex for each vertex using a rotation matrix.

As you probably know, in order to rotate a point around an axis, we multiply the position vector of the bespeak with the rotation matrix. Rotation matrices for rotations around ten, y and z axes are given on the right.

For instance, when nosotros want to rotate a betoken past ninety degrees effectually the z-axis, which has a coordinate (ane,0,0), we multiply the position vector by a rotation matrix.

Rotating a point around z-axis

We need to construct a rotation matrix to rotate each vertex around the z-axis. Let's me write our DrawPolygon method beginning and explicate it.

          void          DrawPolygon(          int          vertexNumber,          float          radius,          Vector3 centerPos,          float          startWidth,          float          endWidth)          {          lineRenderer.startWidth          =          startWidth;          lineRenderer.endWidth          =          endWidth;          lineRenderer.loop          =          true          ;          float          angle          =          2          *          Mathf.PI          /          vertexNumber;          lineRenderer.positionCount          =          vertexNumber;          for          (          int          i          =          0          ;          i          <          vertexNumber;          i+          +          )          {          Matrix4x4 rotationMatrix          =          new          Matrix4x4(          new          Vector4(Mathf.Cos(angle          *          i)          ,          Mathf.Sin(bending          *          i)          ,          0          ,          0          )          ,          new          Vector4(          -          i          *          Mathf.Sin(angle          *          i)          ,          Mathf.Cos(angle          *          i)          ,          0          ,          0          )          ,          new          Vector4(          0          ,          0          ,          1          ,          0          )          ,          new          Vector4(          0          ,          0          ,          0          ,          1          )          )          ;          Vector3 initialRelativePosition          =          new          Vector3(          0          ,          radius,          0          )          ;          lineRenderer.SetPosition(i,          centerPos          +          rotationMatrix.MultiplyPoint(initialRelativePosition)          )          ;          }          }        

You may wonder why the synthetic rotation matrix is iv×4. In computer graphics, the 3-dimensional earth is represented as four-dimensional only this topic is not related to our business here. We only apply it as if it is three-dimensional.

We set the position of initial vertex and rotate it using rotationMatrix each fourth dimension and add the center position to it.

The following prototype is an instance of a hexagon which is drawn by this method.

A hexagon which is rendered using line renderer in Unity3D by C# script

If you increase the number of vertex points, this polygon turns to a circle.

A circle which is rendered using line renderer in Unity3D by C# script

Drawing Waves

In this section, we are going to depict a sinusoidal wave, a traveling wave and a standing wave using sine function.

The mathematical role of the sine wave is given by the following:

Sine wave functions

where

Wave number

Here, one thousand is wave number, f is frequency, ω is the angular frequency, λ is wavelength, v is the linear speed, t is the time and φ is the phase bending. We volition not worry about the phase angle in our discussions.

Sine wave equation with a minus sign represents traveling wave from left to right and the equation with plus sign represents a traveling line wave right to left.

In society to draw a stable sinusoidal wave, we can drop the time office. The following method will draw a sine wave.

          void          DrawSineWave(Vector3 startPoint,          float          amplitude,          float          wavelength)          {          float          10          =          0f;          bladder          y;          float          k          =          two          *          Mathf.PI          /          wavelength;          lineRenderer.positionCount          =          200          ;          for          (          int          i          =          0          ;          i          <          lineRenderer.positionCount;          i+          +          )          {          10          +          =          i          *          0.001          f          ;          y          =          aamplitude          *          Mathf.Sin(thousand          *          x)          ;          lineRenderer.SetPosition(i,          new          Vector3(x,          y,          0          )          +          startPoint)          ;          }          }        

Our DrawSineWave method takes 3 parameters. They are startPoint which is for setting the start position in world infinite, amplitude which is for setting the amplitude of the wave and wavelength which is for setting the wavelength of the sine wave.

A sine wave which is rendered using line renderer in Unity3D by C# script

To obtain the positions of the corresponding mathematical function, first, we determine the positions on the ten-axis. For each x, we have to calculate the y-position.

To animate this moving ridge, we take to implement fourth dimension to our office every bit follows:

          void          DrawTravellingSineWave(Vector3 startPoint,          float          amplitude,          float          wavelength,          float          waveSpeed)          {          float          10          =          0f;          float          y;          bladder          k          =          2          *          Mathf.PI          /          wavelength;          bladder          w          =          k          *          waveSpeed;          lineRenderer.positionCount          =          200          ;          for          (          int          i          =          0          ;          i          <          lineRenderer.positionCount;          i+          +          )          {          10          +          =          i          *          0.001          f          ;          y          =          amplitude          *          Mathf.Sin(k          *          x          +          w          *          Fourth dimension.fourth dimension)          ;          lineRenderer.SetPosition(i,          new          Vector3(x,          y,          0          )          +          startPoint)          ;          }          }        
A traveling sine wave which is rendered using line renderer in Unity3D by C# script
A standing sine wave which is rendered using line renderer in Unity3D by C# script

This time we accept 4 parameters. The fourth parameter is to set the wave speed. This moving ridge travels to the left since we used plus sign in the part. If nosotros would like to create a wave that travels to the correct, nosotros accept to use the minus sign. Yous should keep in listen that we take to write this method in Update().

To create a standing moving ridge, nosotros have to add two waves which travel to the right and which travel to left.

          void          DrawStandingSineWave(Vector3 startPoint,          float          amplitude,          bladder          wavelength,          bladder          waveSpeed)          {          float          ten          =          0f;          float          y;          float          k          =          ii          *          Mathf.PI          /          wavelength;          bladder          w          =          chiliad          *          waveSpeed;          lineRenderer.positionCount          =          200          ;          for          (          int          i          =          0          ;          i          <          lineRenderer.positionCount;          i+          +          )          {          x          +          =          i          *          0.001          f          ;          y          =          aamplitude          *          (Mathf.Sin(k          *          10          +          w          *          Time.time)          +          Mathf.Sin(one thousand          *          x          -          w          *          Fourth dimension.time)          )          ;          lineRenderer.SetPosition(i,          new          Vector3(10,          y,          0          )          +          startPoint)          ;          }          }        

Drawing Bézier Curves

Bézier curves are parametric curves that are used to create smooth curved shapes. They are widely used in computer graphics. In this section, nosotros are going to encounter how we can depict Bézier curves.

When a Bézier curve is controlled by three points, so it is chosen Quadratic Bézier Curve(the first equation below) and when it is controlled by 4 points, it is chosen Cubic Bézier Curve.

The following script will draw a quadratic Bézier curve using positions p0, p1, and p2. You should create three game objects and assign these objects to respective variables in the script to change the shape of the curve in existent-time.

          using          System.Collections;          using          System.Collections.Generic;          using          UnityEngine;          public          course          BezierScript          :          MonoBehaviour          {          individual          LineRenderer lineRenderer;          public          Transform p0;          public          Transform p1;          public          Transform p2;          void          Start(          )          {          lineRenderer          =          GetComponent<LineRenderer>          (          )          ;          }          void          Update(          )          {          DrawQuadraticBezierCurve(p0.position,          p1.position,          p2.position)          ;          }          void          DrawQuadraticBezierCurve(Vector3 point0,          Vector3 point1,          Vector3 point2)          {          lineRenderer.positionCount          =          200          ;          float          t          =          0f;          Vector3 B          =          new          Vector3(          0          ,          0          ,          0          )          ;          for          (          int          i          =          0          ;          i          <          lineRenderer.positionCount;          i+          +          )          {          B          =          (          one          -          t)          *          (          1          -          t)          *          point0          +          2          *          (          i          -          t)          *          t          *          point1          +          t          *          t          *          point2;          lineRenderer.SetPosition(i,          B)          ;          t          +          =          (          ane          /          (          float          )lineRenderer.positionCount)          ;          }          }          }        
A quadratic Bezier curve which is rendered using line renderer in Unity3D by C# script

Likewise, the following method draws a cubic Bézier curve. This time nosotros need 4 points.

          void          DrawCubicBezierCurve(Vector3 point0,          Vector3 point1,          Vector3 point2,          Vector3 point3)          {          lineRenderer.positionCount          =          200          ;          float          t          =          0f;          Vector3 B          =          new          Vector3(          0          ,          0          ,          0          )          ;          for          (          int          i          =          0          ;          i          <          lineRenderer.positionCount;          i+          +          )          {          B          =          (          one          -          t)          *          (          1          -          t)          *          (          i          -          t)          *          point0          +          3          *          (          1          -          t)          *          (          1          -          t)          *          t          *          point1          +          3          *          (          1          -          t)          *          t          *          t          *          point2          +          t          *          t          *          t          *          point3;          lineRenderer.SetPosition(i,          B)          ;          t          +          =          (          1          /          (          float          )lineRenderer.positionCount)          ;          }          }        
A cubic Bezier Curve which is rendered using line renderer in Unity3D by C# script

Complimentary Drawing using Line Renderer

In this section, we are going to see how nosotros can draw freely using the mouse position. We tin do this by creating a new game object with a line renderer attached. When we press the left mouse button, a new game object is created and each frame the position of the mouse added to the line renderer.

Drawing freely using line renderer in Unity3D

First of all, we demand a prefab to create a new game object when we printing the left mouse push. This is an empty game object with a line renderer component fastened. In addition to this, practice not forget to assign a cloth to the line renderer component. Create a prefab from this game object.

Second, create an empty game object and attach the following script DrawManager.

          using          System.Collections;          using          Organization.Collections.Generic;          using          UnityEngine;          public          form          DrawManager:          MonoBehaviour          {          individual          LineRenderer lineRenderer;          public          GameObject drawingPrefab;          void          Update(          )          {          if          (Input.GetMouseButtonDown(          0          )          )          {          GameObject drawing          =          Instantiate(drawingPrefab)          ;          lineRenderer          =          cartoon.GetComponent<LineRenderer>          (          )          ;          }          if          (Input.GetMouseButton(          0          )          )          {          FreeDraw(          )          ;          }          }          void          FreeDraw(          )          {          lineRenderer.startWidth          =          0.i          f          ;          lineRenderer.endWidth          =          0.i          f          ;          Vector3 mousePos          =          new          Vector3(Input.mousePosition.10,          Input.mousePosition.y,          10f)          ;          lineRenderer.positionCount+          +          ;          lineRenderer.SetPosition(lineRenderer.positionCount          -          1          ,          Camera.main.ScreenToWorldPoint(mousePos)          )          ;          }          }        

When you press the left mouse button, a new game object is instantiated from the prefab which we created before. We get the line renderer component from this game object. And so while we are pressing the left mouse button, we call FreeDraw() method.

In the FreeDraw method, we accept ten and y components of the mouse position and ready the z-position as x. Hither, the mouse position is in the screen space coordinates. But we use world space coordinates in line renderer. Therefore we need to convert mouse position to world space coordinates. In each frame, we too demand to increase the number of points. Since we practice not know how many points nosotros need, we cannot prepare position count before.

References
1-https://docs.unity3d.com/ScriptReference/LineRenderer.html
2-http://www.theappguruz.com/blog/bezier-bend-in-games
three-https://en.wikipedia.org/wiki/Bézier_curve
4-https://en.wikipedia.org/wiki/Sine_wave

searssonst1997.blogspot.com

Source: https://www.codinblack.com/how-to-draw-lines-circles-or-anything-else-using-linerenderer/

0 Response to "how to draw 3d lunging forward at you"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel