[Unity 3D Tutorial] Random Automated Flying Script



In today’s tutorial we will create a script that can be used to make animals or objects fly around in a defined area randomly, but realistically. We will develop it for the flight of a bird, and will specifically address the following factors:
Specifying a target to loosely follow.
Varying direction, animation and movement speed at randomized intervals.
Rotating gradually on the horizontal and vertical planes.
Tilting the body to lean into the turn.
Sending the bird straight to a specified location.

Script: https://github.com/ReCogMission/FirstTutorials

*—-*—-* My Playlists *—-*—-*
Unity Journey Of Discovery (Tutorials): https://www.youtube.com/watch?v=8GE2SqTF76U&list=PLnn2bgFFpnzZDjV5AdVd1WDQETMLTIGzw
The Main Course (Maths and Science Concepts Explained Visually: https://www.youtube.com/watch?v=AkXX2xztdyg&list=PLnn2bgFFpnzZGsvkKjPO-kqMEkkN58fpZ
Eco-Pool Natural Swimming Pool (Build and Progress): https://www.youtube.com/watch?v=_ecDwxXU95M&list=PLnn2bgFFpnzZzR_iZMqtGi160cjMA-xZX
4K Treats (Ultra HD content): https://www.youtube.com/watch?v=jY20DNpj4ag&list=PLnn2bgFFpnzYx-V_wfZctdyAoqsxXL9zE
Finger Snacks (Shorter, Unclassified Videos): https://www.youtube.com/watch?v=flfLVxa22pY&list=PLnn2bgFFpnzZ_Mjp70yPdgjDPUSem2AEJ
*—-*—-*—-*—-*—-*—-*—-*

Background Music: www.bensound.com and YouTube Audio Library

By properly parameterizing the factors involved in the process, the script can easily be applied to a variety of different situations. For example, to make bats fly realistically through the air we simply increase parameters like the maximum movement speed and turning rate. Let’s get cracking.
I’ve got quite a big scene set up already, but it’s mainly a nice backdrop. I’ve placed all the game objects that we won’t be using into one holder object which we can ignore for the purposes of this tutorial. We’ll basically just consider the bird and the bat, and here and there the camera to obtain different views. I’m not going into the detail of the bat and the bird prefabs – I’m assuming you have a prefab with animations set up which you want to be able to send flying around some area of your scene. What is important is that the prefab – the bird in our case – has a Rigidbody component. The physics of the Rigidbody is what we’ll use to move the bird around. Make sure that gravity is disabled. We don’t want the bird to fall towards the ground – we will handle all the movement by script. The bird also needs an Animator with its Controller. I bought the bird and bat I’m using here in the asset store as part of a huge animal bundle for $5. It came with a variety of animations, but I created new animation controllers to include only the two I am interested in – a gliding, or idle flying animation and a flying animation. The controller has one parameter called flySpeed, which we’ll use to transition between the two animations. When flySpeed is larger than 0, or 0.0001 since you can’t use exactly 0 in these rules, the animation will change from gliding to flying, and when flySpeed is just about 0 the animation will change to gliding or idle.
Let’s have a quick look at the animations themselves. Firstly, the gliding animation lets the bird’s wings move ever so slightly in the wind. The flying animation is what you’d expect, really. Moving on to the bat, we again have the Rigidbody with gravity turned off. We also have the animator with its controller, with the same two animations as before, including the flySpeed parameter and the transition rules. So, let’s move on to the script itself.
I’ve kept the structure of the script intact to save some time and also allow you to see the bigger picture through the comments while we’re busy adding to script. I didn’t want to delete the serialized fields, since I could then lose all the parameters already entered for the many birds and bats we’ll be using throughout. But we can actually safely delete these nonserialized ones and add them as we discuss them. First off, the script cannot run without an animator component and a Rigidbody component. We therefore specify upfront that these are required. What this does in the editor is that, should an animator component not already be present on the same game object as the script, it would create one there with default settings. On to the serialized fields.
We want to be able to specify the speed of the bird when it is flying idly, the speed at which it can turn or change direction, how fast or gradual a change from one speed to another is affected, and what percentage of time the bird should be flying idle, or gliding. For example, a big bird gliding high up would be idle a lot of the time, while a smaller one closer to the ground would be a lot more industrious, darting around. Next we use a Vector2 to store some minimum/maximum and from/to type parameters. We will use the range for the animation speed to slow down or speed up the flying animation, which is dependent on the range of the movement speed.

Comments are closed.