Quadratic Function — Unity Application Examples


In this arti­cle we are going to see what a qua­drat­ic func­tion is, its math­e­mat­i­cal expres­sion, its char­ac­ter­is­tics, how to graph it in the Carte­sian plane and what it can be use­ful for in the devel­op­ment of games with exam­ples in Unity.




Mathematical expression of a quadratic function

The poly­no­mi­al expres­sion of a qua­drat­ic func­tion is:

It is com­mon­ly read "f of x", being X the inde­pen­dent vari­able, a, b and c con­stant real numbers.

Characteristics of a quadratic function


The domain is the range of allow­able val­ues for the inde­pen­dent vari­able, com­mon­ly referred to as X.

In the case of the qua­drat­ic func­tion the domain is the set of real numbers.

In oth­er words we can choose any val­ue of X belong­ing to the set of real num­bers and we will find its cor­re­spond­ing val­ue f (X).

Click here to read the arti­cle on num­ber sets.

Graph in Cartesian plane of a quadratic function

The graph of f (X) in the Carte­sian plane is a parabola.

Fig. 1: Graph of a qua­drat­ic func­tion with pos­i­tive "a" coefficient.

Fig. 1: Graph of a qua­drat­ic func­tion with neg­a­tive "a" coefficient.

If you are inter­est­ed you can read more about the Carte­sian sys­tem in this article.

Function Roots

The roots of a qua­drat­ic func­tion are the val­ues of x for which f (x) is worth 0, i.e. the points at which the func­tion touch­es the abscis­sa axis (com­mon­ly the x‑axis).

There can be three cas­es, the func­tion has a root, i.e. there is only one val­ue of x for which the func­tion is worth 0.

The func­tion can have two roots, i.e. there are two dif­fer­ent val­ues of x for which the func­tion is worth 0.

The func­tion may not have roots in the set of real num­bers but in the set of com­plex numbers.

To cal­cu­late the roots of a qua­drat­ic func­tion we use a for­mu­la com­mon­ly known as the Bhaskara formula.

Giv­en the fol­low­ing equation:

The two val­ues of x that sat­is­fy this equa­tion are giv­en by:

The plus-minus sign ( ± ) means that they are two equa­tions, in one we use the plus sign and get X1 and in anoth­er we use the minus sign and get X2.

As an obser­va­tion, if what is inside the square root gives as a result a neg­a­tive num­ber it means that the equa­tion has no solu­tion in the set of real ones, in this case we see graph­i­cal­ly that the parabo­la does not cut the axis of the abscissas.

Some examples in Unity of quadratic function

Draw parabolic trajectories

We might be inter­est­ed in draw­ing par­a­bol­ic tra­jec­to­ries through code, for exam­ple for par­ti­cle sys­tems, or to rep­re­sent the pos­si­ble tra­jec­to­ry a pro­jec­tile can fol­low when launch­ing it.

In the fol­low­ing video I show how a set of GameOb­jects fol­low a par­a­bol­ic path described by two qua­drat­ic func­tions to which the para­me­ters can be adjusted.

Video 1: The spheres fol­low the par­a­bol­ic tra­jec­to­ry described by the qua­drat­ic func­tions in red and green.

Obser­va­tion 1: spheres of the same col­or fol­low the path defined by the same equa­tion only that for one I take the con­stant a as pos­i­tive and in the oth­er as neg­a­tive, for this rea­son they are reflect­ed with respect to the axes.

Note 2: Notice that at the begin­ning and end of the video the val­ue of a is 0, this means that the qua­drat­ic term is annulled and we are left with a lin­ear function.

Arti­cle about lin­ear function

Implement quadratic magnitudes

In physics there are many qua­drat­ic mag­ni­tudes that we might be inter­est­ed in imple­ment­ing in our game.

An exam­ple of this can be motion with con­stant accel­er­a­tion, i.e. objects that move with veloc­i­ty that changes lin­ear­ly. For exam­ple, bod­ies in free fall by the action of grav­i­ty or vehi­cles that accelerate.

Some types of ener­gy such as kinet­ic ener­gy or elas­tic poten­tial ener­gy are pro­por­tion­al to the square of a magnitude.


The qua­drat­ic func­tion is a poly­no­mi­al func­tion of order 2 whose graph in the Carte­sian plane is a parabola.

The roots of the qua­drat­ic func­tion are the val­ues of x in which f (x) = 0 is ful­filled. We can use Bhaskara's for­mu­la to find them.

A qua­drat­ic func­tion can have two dif­fer­ent roots, a root or not have roots in the set of real num­bers. If a qua­drat­ic func­tion does not have real roots, it does not graph­i­cal­ly inter­sect the abscis­sa axis.

There are phys­i­cal mag­ni­tudes as well as tra­jec­to­ries of accel­er­at­ed bod­ies that can be described by qua­drat­ic func­tions, so it is use­ful to have knowl­edge about their char­ac­ter­is­tics and thus be able to imple­ment them cor­rect­ly in Unity.

Scroll to Top
Secured By miniOrange