You are on page 1of 14

Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.

com/blog/unity-3d-tornado-physics

BLOG

Sharp Coder is reader-supported, meaning when you buy through links on our site, we may earn an affiliate commission.

Home › Blog › Unity › Physics


SUBSCRIBE TO OUR NEWSLETTER

Unity 3D Tornado Physics Get notified when there are new tutorials, directly
to your inbox.

0 Comments - Jun 12, 2019 Email address

SUBSCRIBE

In this tutorial, we will be creating a Tornado simulation inside Unity 3D.


LINKS
Unity Asset Store
Get Unity
VPS & Dedicated & Cloud Hosting
Shared Hosting
WordPress All-in-one Toolkit
WordPress Managed Hosting
WordPress Event Plugin

Search

SUGGESTED POSTS
Unity 2D Coin Collecting
Unity Countdown Timer
Unity Cinemachine and Timeline Tutorial
How to Install Unity and Create a New Project
Unity version used in this tutorial: Unity 2018.3.0f2 (64-bit) Unity How to Create a Script
Unity How to Attach a Script or a Component to a
Step 1: Create all the necessary scripts GameObject
YOU DO NOT HAVE TO DO
EVERYTHING YOURSELF Unity Mini Game | CUBEavoid
This tutorial requires 2 scripts:
Search from thousands of ready-to-use
Unity assets. POPULAR POSTS
SC_Caught.cs
Visit Asset Store Unity FPS Controller
Unity 2D Character Controller
//This script is attached automatically to each Object caught in Tornado
1 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics
Third-Person Camera in Unity
using UnityEngine;
Make FPS With Enemy AI in Unity

public class SC_Caught : MonoBehaviour Make a Multiplayer Game in Unity Using PUN 2
{
Unity Openable Classic and Sliding Doors
private SC_Tornado tornadoReference;
private SpringJoint spring; Endless Runner in Unity
[HideInInspector]
public Rigidbody rigid;

// Use this for initialization


void Start()
{
rigid = GetComponent<Rigidbody>();
}

// Update is called once per frame


void Update()
{
//Lift spring so objects are pulled upwards
Vector3 newPosition = spring.connectedAnchor;
newPosition.y = transform.position.y;
spring.connectedAnchor = newPosition;
}

void FixedUpdate()
{
//Rotate object around tornado center
Vector3 direction = transform.position - tornadoReference.transform.position;
//Project
Vector3 projection = Vector3.ProjectOnPlane(direction, tornadoReference.GetRotationAxis());
projection.Normalize();
Vector3 normal = Quaternion.AngleAxis(130, tornadoReference.GetRotationAxis()) * projection;
normal = Quaternion.AngleAxis(tornadoReference.lift, projection) * normal;
rigid.AddForce(normal * tornadoReference.GetStrength(), ForceMode.Force);

Debug.DrawRay(transform.position, normal * 10, Color.red);


}

//Call this when tornadoReference already exists


public void Init(SC_Tornado tornadoRef, Rigidbody tornadoRigidbody, float springForce)
{
YOU DO NOT HAVE TO DO //Make sure this is enabled (for reentrance)
EVERYTHING YOURSELF enabled = true;
Search from thousands of ready-to-use
Unity assets.
//Save tornado reference
Visit Asset Store tornadoReference = tornadoRef;

2 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics
//Initialize the spring
spring = gameObject.AddComponent<SpringJoint>();
spring.spring = springForce;
spring.connectedBody = tornadoRigidbody;

spring.autoConfigureConnectedAnchor = false;

//Set initial position of the caught object relative to its position and the tornado
Vector3 initialPosition = Vector3.zero;
initialPosition.y = transform.position.y;
spring.connectedAnchor = initialPosition;
}

public void Release()


{
enabled = false;
Destroy(spring);
}
}

SC_Tornado.cs

//Tornado script controls tornado physics

using System.Collections.Generic;
using UnityEngine;

public class SC_Tornado : MonoBehaviour


{
[Tooltip("Distance after which the rotation physics starts")]
public float maxDistance = 20;

[Tooltip("The axis that the caught objects will rotate around")]


public Vector3 rotationAxis = new Vector3(0, 1, 0);

[Tooltip("Angle that is added to the object's velocity (higher lift -> quicker on top)")]
[Range(0, 90)]
public float lift = 45;

[Tooltip("The force that will drive the caught objects around the tornado's center")]

YOU DO NOT HAVE TO DO public float rotationStrength = 50;


EVERYTHING YOURSELF
[Tooltip("Tornado pull force")]
Search from thousands of ready-to-use
Unity assets. public float tornadoStrength = 2;

Visit Asset Store


Rigidbody r;

3 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics
List<SC_Caught> caughtObject = new List<SC_Caught>();

// Start is called before the first frame update


void Start()
{
//Normalize the rotation axis given by the user
rotationAxis.Normalize();

r = GetComponent<Rigidbody>();
r.isKinematic = true;
}

void FixedUpdate()
{
//Apply force to caught objects
for (int i = 0; i < caughtObject.Count; i++)
{
if(caughtObject[i] != null)
{
Vector3 pull = transform.position - caughtObject[i].transform.position;
if (pull.magnitude > maxDistance)
{
caughtObject[i].rigid.AddForce(pull.normalized * pull.magnitude, ForceMode.Force);
caughtObject[i].enabled = false;
}
else
{
caughtObject[i].enabled = true;
}
}
}
}

void OnTriggerEnter(Collider other)


{
if (!other.attachedRigidbody) return;
if (other.attachedRigidbody.isKinematic) return;

//Add caught object to the list


SC_Caught caught = other.GetComponent<SC_Caught>();
if (!caught)
{
YOU DO NOT HAVE TO DO caught = other.gameObject.AddComponent<SC_Caught>();
EVERYTHING YOURSELF }
Search from thousands of ready-to-use
Unity assets.
caught.Init(this, r, tornadoStrength);
Visit Asset Store
if (!caughtObject.Contains(caught))

4 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics
{
caughtObject.Add(caught);
}
}

void OnTriggerExit(Collider other)


{
//Release caught object
SC_Caught caught = other.GetComponent<SC_Caught>();
if (caught)
{
caught.Release();

if (caughtObject.Contains(caught))
{
caughtObject.Remove(caught);
}
}
}

public float GetStrength()


{
return rotationStrength;
}

//The axis the caught objects rotate around


public Vector3 GetRotationAxis()
{
return rotationAxis;
}

//Draw tornado radius circle in Editor


void OnDrawGizmosSelected()
{
Vector3[] positions = new Vector3[30];
Vector3 centrePos = transform.position;
for (int pointNum = 0; pointNum < positions.Length; pointNum++)
{
// "i" now represents the progress around the circle from 0-1
// we multiply by 1.0 to ensure we get a fraction as a result.
float i = (float)(pointNum * 2) / positions.Length;

YOU DO NOT HAVE TO DO // get the angle for this step (in radians, not degrees)
EVERYTHING YOURSELF float angle = i * Mathf.PI * 2;
Search from thousands of ready-to-use
Unity assets.
// the X & Y position for this angle are calculated using Sin & Cos
Visit Asset Store float x = Mathf.Sin(angle) * maxDistance;
float z = Mathf.Cos(angle) * maxDistance;

5 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

Vector3 pos = new Vector3(x, 0, z) + centrePos;


positions[pointNum] = pos;
}

Gizmos.color = Color.cyan;
for (int i = 0; i < positions.Length; i++)
{
if (i == positions.Length - 1)
{
Gizmos.DrawLine(positions[0], positions[positions.Length - 1]);
}
else
{
Gizmos.DrawLine(positions[i], positions[i + 1]);
}
}
}
}

Step 2: Creating a Tornado


1. Create Tornado particles:

• Create a new GameObject (GameObject -> Create Empty) and name it "Tornado"
• Create another GameObject and name it "Particles", move it inside "Tornado" and change its position to (0, 0, 0)
• Add ParticleSystem component to "Particles" GameObject
• In Particle System enable these modules: Emission, Shape, Velocity over Lifetime, Color over Lifetime, Size
over Lifetime, Rotation over Lifetime, External Forces, Renderer.

YOU DO NOT HAVE TO DO


EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.

Visit Asset Store

6 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

2. Assign the values for each Particle System module (Check Screenshots below):

Main (Particles) module:

Emission module:

YOU DO NOT HAVE TO DO


EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.
Shape module:

Visit Asset Store

7 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

Velocity over Lifetime module:

Color over Lifetime module:

(2DO
YOU DO NOT HAVE TO Gray colors at each end and 2 White colors in the inner part)
EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.
Size over Lifetime module:

Visit Asset Store

8 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

(Size over Lifetime uses curve which looks like this)

(Size slightly goes down then goes up)

Rotation over Lifetime:

External Forces module:

This module does not need any changes, just leave the default values.

Renderer module:

For this module we only need to assign the material:

• Create a new material and call it "tornado_material"


YOU DO NOT HAVE TO DO
• Change
EVERYTHING YOURSELF its Shader to "Legacy Shaders/Particles/Alpha Blended"
Search from thousands of• ready-to-use
Assign the texture below to it (or click here):
Unity assets.

Visit Asset Store

9 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

• Assign the tornado_material to a Renderer module:

Now Tornado particles should look something like this:

YOU DO NOT HAVE TO DO


EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.

Visit Asset Store

10 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

But as you can see it does not look like Tornado at all, that's because we have one more component to add,
which is Particle System Force Field, this component is needed to simulate the circular wind:

• Create a new GameObject and name it "ForceField"


• Move "ForceField" inside "Tornado" GameObject and change it's position to (0, 0, 0)

• Add Particle System Force Field component to "ForceField"


• Change the values of the Force Field component to the same as on the screenshot below:

YOU DO NOT HAVE TO DO


EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.
Now the particles should look something like this, which is much better:
Visit Asset Store

11 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

3. Setting Up Tornado Physics

• Add Rigidbody and SC_Tornado components to "Tornado" GameObject

• Create a new GameObject and name it "Trigger"


• Move "Trigger" inside "Tornado" GameObject and change its position to (0, 10, 0) and change its scale to (60, 10,
60)
• Add MeshCollider component to "Trigger" GameObject, check the Convex and IsTrigger checkboxes, and change
YOU DO NOT HAVE TO
itsDO
Mesh to default Cylinder
EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.

Visit Asset Store

12 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics

The tornado is now ready!

To test it simply create a Cube and add a Rigidbody component to it, then place it somewhere inside the Trigger area.

Once you press Play the Cube should be pulled in by the Tornado:

unity3d unity physics physx tutorial rigidbody tornado simulation

Source

� TornadoSystem.unitypackage 239.71 KB

← Unity 3D Rigidbody Magnet Unity Drag Rigidbodies With a


Mouse Cursor →

YOU DO NOT HAVE TO DO


Sharp Coder
EVERYTHING YOURSELF
PRIVACY
0 Comments
Search from thousands ofTERMS OF USE
ready-to-use
Unity assets. CONTACT US
COLOR PICKER
Visit Asset RANDOM
Store WORD PICKER
ABOUT
Copyright © 2021
13 of 14 10/18/21, 2:51 PM
Unity 3D Tornado Physics | Sharp Coder Blog https://sharpcoderblog.com/blog/unity-3d-tornado-physics
Follow us on social media

YOU DO NOT HAVE TO DO


EVERYTHING YOURSELF
Search from thousands of ready-to-use
Unity assets.

Visit Asset Store

14 of 14 10/18/21, 2:51 PM

You might also like