Close

Hit ESC to close

A slingshot is normally a small hand-powered projectile weapon. The classic form consists of a Y-shaped frame held in the off hand, with two natural-rubber strips attached to the uprights. In this tutorial, we will see how to make a slingshot in Unity.

(Source)

Prerequisites:

Unity 5 or higher. 

 

(Source)

This tutorial covers following topics:

#1 Slingshot Setup:

The first step is to set the camera projection to orthographic and its positions to (0 f,1f,-10f).

To make a slingshot in Unity, first, we need to make its two rubber strips that hold the ball.We will make these strips using Line Renderer in Unity. The Line Renderer is a component in Unity that takes an array of two or more points in 3D space and draws a straight line between each one.

Create an empty gameobject and name it “Left Line”. Add Line Renderer component to it from AddComponent -> Effects -> Line Renderer. Create anther empty gameobject and name it “Right Line” and add Line Renderer to it. Now you can see pink color lines in your Game view.

As a slingshot requires to drag a ball or any object from its drag point to throw, we need to create a drag point from where we can drag a ball to throw. Create an empty gameobject and name it “ObjectHolder”. Add a BoxColloder to it.To make this dragPoint dragable we need to write some code. Create a new script and name it “DragObject” and add the following lines of code in it :

 

Attach this script to the “ObjectHolder” gameobject and hit play. You can drag the sphere with the mouse.

Now we need to adjust the lines with our drag point position. Set the position of “ObjectHolder” to (0f,-2.36f,4f).

To make line adjustable with drag point, we need to define the anchors from where to stretch the line to the drag point position.

Create an empty game object and name it “Left Anchor”.Set its position to ( -0.9f, -2.28f ,4.9f).

Create another empty gameobject  and name it “Right Anchor”. Set its position to ( -0.9f, -2.28f ,4.9f).

Now create a script called SlingShotManager and following lines of code in it :

Create an empty game object and name it “SlingShot”. Set its position to (0f,-3.27f,5f) and attach this script to the ”SlingShot” and hit play. Drag the “ObjectHolder”, and you can see the lines are adjusting to the drag point.

#2 Shoot Ball: 

Now create a sphere and name it “Ball”.

Set its position to (0, -2.366f, 4.189f) and rotation to (0 ,0 ,0) and scale(0.3,0.3,0.3) . To throw a ball, we need to manage some physics value according to the input and also need to manage the direction of the ball to throw. Create an empty gameobject and name it “aimer” as a child of “ObjectHolder” and set its position to (0,-0.031f,1.81f). It will be used for aiming of the ball to throw. Add Rigidbody component to the “Ball”. Also, we need to display the path of the ball to shoot. Create a sphere and name it “Point”. Create a Material name it “path mat” and set its shader to Unlit/Color and choose the color of your choice. Assign this material to the point gameobject. Create the total number of points you want to display in the path.

Update the script “SlingShotManager” with the following lines of code :

Now we need to adjust path according to the “ObjectHolder” so update the script “DragObject” with following lines of code:

Hit play and you can see the path is adjusting as you drag the “Object holder”.

You can set the Materials to all the gameobjects, so they look good. Create a material and name it “Line Mat” set its shader to “Unlit/Color” .Give the color of your choice and add this material to “Left Line” and “Right Line”.  Create a material and name it “Ball Mat” and add it in “Ball”.  Add a component sprite Renderer to ObjectHolder game object and assign the sprite “Object Holder” to it. Also, add component Sprite Renderer to “SlingShot” game object and assign the sprite “Sling Shot” to it. Both sprites are in the demo project.

Now can see the SlingShot of your selected color with the ball. Hit play and throw the ball with the slingshot.

Final output:

Full Source Code

Download APK

 

A Particle system is a component of Unity that is used to add some extra effects in your game like fire, flowing water, smoke etc. which is not possible through the normal game object having meshes and textures. A particle system generally emits particles in random positions within a predefined space.

Particles are small, simple images or meshes that are displayed and moved in great numbers by a particle system.

In this tutorial, we will implement a particle system that will look like confetti. Prerequisites:

  1. Unity 5 or higher.
  2. How to add a new Particle System?
  3. Manipulating Particle System

How to add a new Particle System?

To add a new particle system click on GameObject in Menu and then select Particle System. As it is a component you can also add it to an empty game object from Component ->Effects -> Particle System. Now select particle system from scene hierarchy. You can see a Particle Effect dialogue in the scene view which allows you to Simulate and Stop the particle system. Set the camera’s clear flag to Solid colour and set colour black. Also, set the projection to Orthographic so we can see the effect of the particle clearly. Also set camera position to (0,1,0).

Manipulating Particle System

To create a confetti-like particle system we need to manipulate the default particle system. Create a new particle system and name it “Confetti”. Set its position to (0,-1.87,5)and rotation to (-90,0,0). The default particle system has several sub-sections as shown in the inspector.0

First of all, create a material and name it ConfettiMat and set its shader to Particles ->Anim Alpha Blended. Set it in the Renderer section of the particles system in the inspector. This applies a material to a particles system and renders it in-camera.

Now set the following values :

1. Duration: It is The length of time in seconds for the particle to run. Set this value to 0.1.

2. Looping: if true, the emission cycle will repeat after the duration.Set it true.

3. Start Delay: The delay in seconds before the particle system start emitting. Set this value to 0.

4. Start Lifetime: The initial lifetime in seconds for the particles. The particle is destroyed after this elapsed time. Set this value to 8.

5. Start Speed: The initial speed of the particles. The greater the speed of the particles, the more spread out they will be. Set this value to 20.

6. Start Size: the initial size of the particles. Set this value to 0.25.

7. Start Rotation: The initial rotation angle of the particles. Set this value to 0.

8. Start Color: The initial colour of the particles. Set the type of value to the gradient. Click on the colour part, it will open a Gradient Editor window. Set the colour values as shown in the image. You can also set it as you want.

9. Gravity Modifier: Scales the gravity value set in Unity’s Physics Manager window. If it’s set to 0, the gravity will be turned off. Set this value to 3.

10. Play On Awake: Starts emitting immediately when enabled. If this is turned off, you have to manually start the particle system via script or an animation system. Leave this setting on.

11. Max Particles: The maximum amount of particles the particle system is allowed to have alive at any time. Set this value to 300.

12. Emission: The Emission module is one of the most important modules in Unity particle systems; it handles the number and timing of emitted particles in the system to create a continuous flow or a sudden burst of particles depending on your needs.

13. Rate over time: It is the number of particles emitted per second (Time), or alternatively, per unit (Distance) 300

14. Shape: The Shape module, as the name implies, controls the shape and the behaviour of particles in that shape. Set its value as follows:

15. Force over Lifetime: Controls the force of each particle during its lifetime. Set its value as shown in the picture:

16. Rotation over a lifetime: Controls the angular velocity of each particle during its lifetime. Set the value as shown below:

17. Collision: This allows you to specify multiple collision planes that the particles can collide with. Create an empty gameobject and name it Surface. Set its position to (0,-4.1,5) and add the BoxCollider component to it. This will allow the particles to collide with the surface and create a real-time confetti-like physics effect.

You can create as many planes as you want for particle collision. You can also rotate that plane using rotation tools in

particle system -> collision -> scale plane

Click on the simulate button, and you can see colourful particles like confetti. Now create a Cylinder game object and set its position to (0,-2.81,4) and scale to (1,1.2,1).

Now hit the play button, and you can see confetti particles coming out from the cylinder.

In this blog post, We try to explain How to Get data from AssetBundle on The Server and Data From AssetBundle In Local.

What Is An AssetBundle?

An AssetBundle is a collection of assets from a project saved in a compact file with the purpose of being loaded separately to the application. AssetBundles can be loaded on demand by a game or application built in Unity. This allows streaming and asynchronous loading of content such as models, textures, audio clips, or even entire scenes. AssetBundles can be “pre-cached” and stored locally for immediate loading when first running an application. AssetBundles should be built for each target platform separately.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

What Is The Use Of It?

AssetBundles can be used for downloadable content (DLC), reducing initial install size, loading assets optimized for the end-users platform, and reduce runtime memory pressure. The primary purpose of AssetBundles, however, is to stream content on demand from a remote location, to be loaded into the application as necessary. AssetBundles can contain any kind of asset type recognized by Unity, including custom binary data. The only exception is that script assets are not allowed.

#1

How To Get Data From AssetBundle On The Server?

Step-1: Build An AssetBundle

Now, right click on the SoundAsset and click on the Build AssetBundle. Choose the location to save the AssetBundle and save it. The asset bundle will be available on the location for Android and as well as iOS. BuildPipeline.BuildAssetBundle will build the selected AssetBundle for the platform specified in BuildTarget. Now upload the AssetBundle to the Server.

To make an AssetBundle, first, we need to build the asset of unity project.You can manually mark an asset as AssetBundle. Select any asset from project view and in the bottom, part from model view give AssetBundle name. You can also set version of AssetBundle.

In this tutorial, we will load an asset that contains sound files. The project contains a SoundAsset prefab inside Assets folder. We will use this to create an AssetBundle, Load it into the project and play the sound.

Create a script “ExportAssetBundles” inside Editor folder and add following lines of code in it:

Step-2: Load AssetBundle From The Server

We will load the SoundAsset from the server and play the Audio clip attached to it.
Create a new script name it “AssetBundleSample ”. It will manage all the loading operation of asset bundle. Add the following lines code in it:

Attach this script to an empty game object. create a Text to display loading status and assign to this script. Also, give the server URL of AssetBundle.

Now create 3 buttons and add LoadSoundFiles() method in the OnClick listener. Pass “Music1” as a parameter in button1, ”Music2” as a parameter in button2 and ”Music2” and “Music3 as a parameter in button3”. Now hit play. The loading will display as “%” format. when done with loading, it will first instantiate the AssetBundle and then will play the Audio Clip attached to it.

#2

How To Get Data From AssetBundle In Local?

In this example, we will create a scene as Asset bundle and load it from local memory. Create a scene and name it “AssetScene”. Add some images and other game objects to this scene you want.

Step-1: Build A Scene As AssetBundle

To create a scene as asset bundle, update the following lines of code to the “ExportAssetBundles” script:

This script will allow us to create an asset bundle from the scene. Right click on AssetScene in project view and click Build Scene Bundle. It will create an asset Bundle inside Assets/AssetBundle folder.

Step-2: Load AssetBundle From The Local

To load any files from local memory in any platform, first, we need to save it somewhere in the local machine. Upload this AssetScene to the server.

Update the AssetBundleSample Script with following lines of code:

Create a button name it Loads scene and adds LoadFromLocal() method in the OnClick listener. LoadSceneBundle will load the Scene Asset from the server and store it in the local memory of the device. Call this coroutine in Start() method. Now when you click on LoadScene button, you will be navigated to the SceneBundle. You do not need to add this scene to build setting.

Full Source Code

Download APK

 

 

Animation Clips are one of the core elements of Unity’s animation system. Gameobjects in Unity can be animated using animation Clip. Using Animation Clips, we can change position, rotation and scale of a gameobject as well as other properties like float or integer value of material from the script.

 

In Unity, Animation clips are created using “Animation“ window, under Windows from the main menu -> Animation. You can see an animation window like the game view and scene view in unity editor :

 

 

All animations in Unity are handled by the Animation Controller Component. This is the component that references to 1 or more animation clips to animate a gameobject.

 

To create an animation clip to scale a gameobject we need to modify the scale property of that gameobject. Create a Cube. Select that Cube in the hierarchy, and you can see there is a button in Animation window “Create” to create a new animation clip :

 

 

By pressing the create button in the Animation view, a new animation clip is created. Unity will ask to pick the name & location to save this new Animation Clip. Unity also creates an Animator Controller asset with the same name as the selected GameObject, adds an Animator component to the GameObject, and connects the assets up appropriately. Give the name Scale and save the clip. Unity will automatically create an animation clip named “Scale_Anim“ and an animator controller named Cube. It is the name of the currently selected gameobject. The animation clip contains the .anim extension. Now can see animation window with frames.

 

 

Now select the gameobject Cube and click on “ Add Property ” button.Select scale form Transform and press “ + ” button to create scale a property. The upper part number defines the playback duration and time line of the animation clip.You can click anywhere on the Animation window time line to move the playback head to that frame, and preview or modify that frame in the Animation Clip. The numbers in the time line are shown as seconds and frames, so 1:30 means 1 second and 30 frames.Default keyframes are created from 0:00 to 1:00. We can add new keyframes by right clicking in the animation window, you will see a menu and then select Add key. You can drag this key anywhere in the animation window to create an animation clip.

 

There are 2 modes of animation window to create an animation clip :

Dopesheet - Dopesheet allows us to create animation clips with some exact specific values for the gameobject.

Curves - Curves allows us to create animation clips with curves value.

When you are creating an animation clip, the animation window will record the animation. The red button at the top left corner is highlighted. In this tutorial, we will use Dopesheet to create an animation clip.

We will scale cube from (1,1,1) to (2,2,2) using animation clip. Select the first keyframe at 0:00 and then select cube from hierarchy and set scale value from Transform component to (1,1,1). This will set the value of cube scale to (1,1,1) at the beginning of the clip. Now add a new keyframe at the time 0:50 and set the scale value of cube to (2,2,2). This will set the cube scale to (2,2,2) in the middle of the animation. Now select the last keyframe and set the scale to again (1,1,1). This will set the value at last of animation.

 

Now hit the play in the animation window to preview the animation. You can see scale animation of the cube. This animation clip is assigned as a default state in the animation controller.

 

At last, you will see the keyframes value.

 

 

Now hit the play button in Unity editor. You will see a Zoom-in Zoom-out animation for the cube.

 

Now create another clip for rotation.Click in the dropdown of animation list below the record button and then select Create New Clip. Save clip as Rotate_Anim.

 

 

We will rotate Cube from 0 to 360 degrees. Select the first keyframe at 0:00 and then select cube from hierarchy and set rotation value from Transform component to (0,0,0). This will set the value of cube rotation to (0,0,0) at the beginning of the clip. Now add a new keyframe at the time 0:50 and set the rotation value of cube to (0,0,180). This will set the cube rotation to (0,0,180) at the middle of the animation. Now select the last keyframe and set the rotation to (0,0,360). This will set the value at last of animation.

 

Now hit the play in the animation window to preview the animation. You can see rotation animation of cube. This animation clip is assigned as a new animation state in the animation controller.

 

To play both animations for the cube, we need to make a transition between these 2 clips. Add a transition from Scale_anim to Rotate_Anim in animator controller. To make a transition right click on the current state and select Make Transition and then drag towards the destination state and then release. You will see a transition with arrow line. Now add a transition from Rotate_Clip to Scale_Clip. 

 

 

Now hit the play button in the Unity Editor. You will see the scale animation and rotation animation of the cube.

Final output:

Full Source Code

Download APK

 

Photon is a real-time multiplayer mobile game development agency framework which has server and cloud services. Games are hosted in globally distributed Photon Cloud so we do not need to worry about hosting. All the operations related to multiplayer functionality are handled by Photon. Free version of PUN comes with 20 CCU subscription but you can upgrade it as you need it. More info can be found on their official site (https://www.photonengine.com/en-US/Photon).

In this tutorial we are going to cover in detail the integration of Photon Unity Networking (PUN) with Unity.

Prerequisites

1. Unity 5 or later , you can download from here (https://unity3d.com/)

2. Photon Unity Networking (PUN) package, you can download free package from Asset store from here (https://www.assetstore.unity3d.com/en/#!/content/1786)

 

In this tutorial we are using Unity 5.6.3.

We have following major things in this blog:

1. Setup photon in Unity project

2. Manage connection with photon

3. Synchronization of players

4. Testing

#1 Configure Game on Photon:

Step-1: Login to your account

We need an account on Photon to configure our game .Go to https://www.photonengine.com/en-US/Photon. Log in to your account if you already have otherwise you can click on Register to create new account.

 

Step-2 : Create and configure game

Provide necessary info related to your game and create game.

 

Step-3 : Get your App Id from dashboard

We have completed the setup on Photon side.

 

#2 Setup Photon in Unity Project:

Before we start, download the PUN package from the Asset store (https://www.assetstore.unity3d.com/en/#!/content/1786). After download, open PUN settings to configure the project for photon. You can find PUN settings under window -> Photon Unity Networking -> Highlight server settings.

 

It will open a setting wizard that allows to configure project with photon. Add your app in in the App id field of PhotonServerSettings. Set your appropriate region. Check AutoJoinLobby so photon will automatically join the player in the lobby.

 

#3 Manage connection with Photon:

Create a new script called PUNManager and add it to an empty gameobject. Using this script we will handle all server side operations. First of all, we need to connect with Photon. This will enable us to host and join rooms of our game based on the AppID. Call PhotonNetwork.ConnectUsingSettings () method to make connection. We can pass the version number of the game as a string in the argument. Import namespace Photon.

Use following code to connect your project with photon:

To check status whether we are connected or not, take a Text and show status using PhotonNetwork. connectionStateDetailed. After connection, photon will automatically join player in a lobby. When a player joins a lobby OnJoinedLobby callback is called. After that we need to connect to a room. PhotonNetwork.JoinRandomRoom  is allow to connect players in a random available room. If no room available it will create a new one. We can also use PhotonNetwork.JoinRoom to connect if we already know the room name. In this tutorial we will use JoinRandomRoom. We will create a button that will handle this event. Add the JoinRoom method in buttons click event in Inspector. After connecting to a room, the function OnJoinedRoom is called.

Add following code to PUNManager script:

#4 Synchronization:

As now we are able to connect multiple players, add some game coding to the project. Create a plane and position it with (0, -0.5f, 0). Create a cube, name it Player. Create a script PlayerManager that will handle players’ movements.

Add following code in PlayerManager:

Add this script to Player gameobject. Next, add the Photon view component to the player object (Component > Photon View).

This will enable us to send data packages over the network to synchronize the player. Observe option is automatically set to “reliable delta compressed”. This means that synchronized data will be sent automatically, but only if its value changed. So for example if you move as a player, your position will be updated on the server. By setting it to “off” there is no automatic synchronization at all and you have to do it manually. Drag the transform component into the observe field. At the bottom the field serialization is now added, where you can state which values from the transform to synchronize. Set its value to “Only Position”. In the project hierarchy, create a folder called “Resources”. In it, place the player object from the scene to make it a prefab. This is needed to instantiate the object on the network. In the PUNManager-script add a public game object variable for the player prefab. When OnJoinedRoom() is called, we want to create the player object. Instantiating on a Photon network requires the prefab to be placed in the Resources folder. The prefab parameter for PhotonNetwork.Instantiate() is a string instead of a gameobject. Also create a gameobject plane that can be activated when connected to room.

Make a build of project and run both. You can control both players movement, not just your own. To Solve this issue, we need to check is this a player that instantiated the object? To implement this , we need to check whether the object on the network “is mine” in the player script. Add using Photon; namespace and replace Monobehaviour class with PunBehaviour:

Again make a build and test your code. You can now control only one of the players.

#5 State Synchronization:

For synchronization between players, photon provides a callback OnPhotonSerializeView() that constantly updates values over the network. This method is useful to sync data that changes often like player movement.

Go to the Photon view component on the player’s prefab. The observed field contains the component that will be synchronized. Drag the component of the player script in the observed field.

Add OnPhotonSerializeView() to the player script.

This function is automatically called every time it either sends or receives data. If the user is the one updating the object, he/she writes to the stream. The clients receive the stream and can apply the data that was send to the object. In the example below, the user sends the transform’s position with stream.SendNext() and this is received by the clients with stream.ReceiveNext(). The order of data send should be the same as the order in which the data is received, otherwise values will get mixed up.

#6 Remote Procedure Calls:

Another method of network communication is Remote Procedure Calls (RPCs), which is more useful for data that does not constantly change. This approach is only able to send integers, floats, strings, vectors and quaternions. An RPC is sent by calling photonView.RPC(), in which you define the function name and parameters. Also the Photon targets are required: “PhotonTargets.All” sends the data to all players including local player, “PhotonTargets.OthersBuffered” to everyone in the room except yourself. Add [PunRPC] before function implementation to make it a Remote Procedure Call. To implement this we will create a method that will change the color of the material of your own player.

Add following lines of code in PlayerManager script:

To send data to all the players in room we need to use PhotonTargets. All as target when calling a RPC method. Take a Text and a button that will handle this event. Add PhotonView component to the PUNManager gameobject from Component > Photon View. Create a method named ChangeTextColor and add it in button click event in inspector.

Add the following code in the PUNManager script:

Make a new build and test it. Click on the “Change color ” button and you can see that the text color is changing on both side.

#7 Testing

Now create another build and if you run the game, you will see the player’s colors can be changed. Also you can see the other player’s movement.

You can find the complete code here:

Full Source Code

Download APK

 

A slingshot is normally a small hand-powered projectile weapon. The classic form consists of a Y-shaped frame held in the off hand, with two natural-rubber strips attached to the uprights. In this tutorial, we will see how to make a slingshot in Unity.

(Source)

Prerequisites:

Unity 5 or higher. 

 

(Source)

This tutorial covers following topics:

#1 Slingshot Setup:

The first step is to set the camera projection to orthographic and its positions to (0 f,1f,-10f).

To make a slingshot in Unity, first, we need to make its two rubber strips that hold the ball.We will make these strips using Line Renderer in Unity. The Line Renderer is a component in Unity that takes an array of two or more points in 3D space and draws a straight line between each one.

Create an empty gameobject and name it “Left Line”. Add Line Renderer component to it from AddComponent -> Effects -> Line Renderer. Create anther empty gameobject and name it “Right Line” and add Line Renderer to it. Now you can see pink color lines in your Game view.

As a slingshot requires to drag a ball or any object from its drag point to throw, we need to create a drag point from where we can drag a ball to throw. Create an empty gameobject and name it “ObjectHolder”. Add a BoxColloder to it.To make this dragPoint dragable we need to write some code. Create a new script and name it “DragObject” and add the following lines of code in it :

 

Attach this script to the “ObjectHolder” gameobject and hit play. You can drag the sphere with the mouse.

Now we need to adjust the lines with our drag point position. Set the position of “ObjectHolder” to (0f,-2.36f,4f).

To make line adjustable with drag point, we need to define the anchors from where to stretch the line to the drag point position.

Create an empty game object and name it “Left Anchor”.Set its position to ( -0.9f, -2.28f ,4.9f).

Create another empty gameobject  and name it “Right Anchor”. Set its position to ( -0.9f, -2.28f ,4.9f).

Now create a script called SlingShotManager and following lines of code in it :

Create an empty game object and name it “SlingShot”. Set its position to (0f,-3.27f,5f) and attach this script to the ”SlingShot” and hit play. Drag the “ObjectHolder”, and you can see the lines are adjusting to the drag point.

#2 Shoot Ball: 

Now create a sphere and name it “Ball”.

Set its position to (0, -2.366f, 4.189f) and rotation to (0 ,0 ,0) and scale(0.3,0.3,0.3) . To throw a ball, we need to manage some physics value according to the input and also need to manage the direction of the ball to throw. Create an empty gameobject and name it “aimer” as a child of “ObjectHolder” and set its position to (0,-0.031f,1.81f). It will be used for aiming of the ball to throw. Add Rigidbody component to the “Ball”. Also, we need to display the path of the ball to shoot. Create a sphere and name it “Point”. Create a Material name it “path mat” and set its shader to Unlit/Color and choose the color of your choice. Assign this material to the point gameobject. Create the total number of points you want to display in the path.

Update the script “SlingShotManager” with the following lines of code :

Now we need to adjust path according to the “ObjectHolder” so update the script “DragObject” with following lines of code:

Hit play and you can see the path is adjusting as you drag the “Object holder”.

You can set the Materials to all the gameobjects, so they look good. Create a material and name it “Line Mat” set its shader to “Unlit/Color” .Give the color of your choice and add this material to “Left Line” and “Right Line”.  Create a material and name it “Ball Mat” and add it in “Ball”.  Add a component sprite Renderer to ObjectHolder game object and assign the sprite “Object Holder” to it. Also, add component Sprite Renderer to “SlingShot” game object and assign the sprite “Sling Shot” to it. Both sprites are in the demo project.

Now can see the SlingShot of your selected color with the ball. Hit play and throw the ball with the slingshot.

Final output:

Full Source Code

Download APK

 

A Particle system is a component of Unity that is used to add some extra effects in your game like fire, flowing water, smoke etc. which is not possible through the normal game object having meshes and textures. A particle system generally emits particles in random positions within a predefined space.

Particles are small, simple images or meshes that are displayed and moved in great numbers by a particle system.

In this tutorial, we will implement a particle system that will look like confetti. Prerequisites:

  1. Unity 5 or higher.
  2. How to add a new Particle System?
  3. Manipulating Particle System

How to add a new Particle System?

To add a new particle system click on GameObject in Menu and then select Particle System. As it is a component you can also add it to an empty game object from Component ->Effects -> Particle System. Now select particle system from scene hierarchy. You can see a Particle Effect dialogue in the scene view which allows you to Simulate and Stop the particle system. Set the camera’s clear flag to Solid colour and set colour black. Also, set the projection to Orthographic so we can see the effect of the particle clearly. Also set camera position to (0,1,0).

Manipulating Particle System

To create a confetti-like particle system we need to manipulate the default particle system. Create a new particle system and name it “Confetti”. Set its position to (0,-1.87,5)and rotation to (-90,0,0). The default particle system has several sub-sections as shown in the inspector.0

First of all, create a material and name it ConfettiMat and set its shader to Particles ->Anim Alpha Blended. Set it in the Renderer section of the particles system in the inspector. This applies a material to a particles system and renders it in-camera.

Now set the following values :

1. Duration: It is The length of time in seconds for the particle to run. Set this value to 0.1.

2. Looping: if true, the emission cycle will repeat after the duration.Set it true.

3. Start Delay: The delay in seconds before the particle system start emitting. Set this value to 0.

4. Start Lifetime: The initial lifetime in seconds for the particles. The particle is destroyed after this elapsed time. Set this value to 8.

5. Start Speed: The initial speed of the particles. The greater the speed of the particles, the more spread out they will be. Set this value to 20.

6. Start Size: the initial size of the particles. Set this value to 0.25.

7. Start Rotation: The initial rotation angle of the particles. Set this value to 0.

8. Start Color: The initial colour of the particles. Set the type of value to the gradient. Click on the colour part, it will open a Gradient Editor window. Set the colour values as shown in the image. You can also set it as you want.

9. Gravity Modifier: Scales the gravity value set in Unity’s Physics Manager window. If it’s set to 0, the gravity will be turned off. Set this value to 3.

10. Play On Awake: Starts emitting immediately when enabled. If this is turned off, you have to manually start the particle system via script or an animation system. Leave this setting on.

11. Max Particles: The maximum amount of particles the particle system is allowed to have alive at any time. Set this value to 300.

12. Emission: The Emission module is one of the most important modules in Unity particle systems; it handles the number and timing of emitted particles in the system to create a continuous flow or a sudden burst of particles depending on your needs.

13. Rate over time: It is the number of particles emitted per second (Time), or alternatively, per unit (Distance) 300

14. Shape: The Shape module, as the name implies, controls the shape and the behaviour of particles in that shape. Set its value as follows:

15. Force over Lifetime: Controls the force of each particle during its lifetime. Set its value as shown in the picture:

16. Rotation over a lifetime: Controls the angular velocity of each particle during its lifetime. Set the value as shown below:

17. Collision: This allows you to specify multiple collision planes that the particles can collide with. Create an empty gameobject and name it Surface. Set its position to (0,-4.1,5) and add the BoxCollider component to it. This will allow the particles to collide with the surface and create a real-time confetti-like physics effect.

You can create as many planes as you want for particle collision. You can also rotate that plane using rotation tools in

particle system -> collision -> scale plane

Click on the simulate button, and you can see colourful particles like confetti. Now create a Cylinder game object and set its position to (0,-2.81,4) and scale to (1,1.2,1).

Now hit the play button, and you can see confetti particles coming out from the cylinder.

In this blog post, We try to explain How to Get data from AssetBundle on The Server and Data From AssetBundle In Local.

What Is An AssetBundle?

An AssetBundle is a collection of assets from a project saved in a compact file with the purpose of being loaded separately to the application. AssetBundles can be loaded on demand by a game or application built in Unity. This allows streaming and asynchronous loading of content such as models, textures, audio clips, or even entire scenes. AssetBundles can be “pre-cached” and stored locally for immediate loading when first running an application. AssetBundles should be built for each target platform separately.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

What Is The Use Of It?

AssetBundles can be used for downloadable content (DLC), reducing initial install size, loading assets optimized for the end-users platform, and reduce runtime memory pressure. The primary purpose of AssetBundles, however, is to stream content on demand from a remote location, to be loaded into the application as necessary. AssetBundles can contain any kind of asset type recognized by Unity, including custom binary data. The only exception is that script assets are not allowed.

#1

How To Get Data From AssetBundle On The Server?

Step-1: Build An AssetBundle

Now, right click on the SoundAsset and click on the Build AssetBundle. Choose the location to save the AssetBundle and save it. The asset bundle will be available on the location for Android and as well as iOS. BuildPipeline.BuildAssetBundle will build the selected AssetBundle for the platform specified in BuildTarget. Now upload the AssetBundle to the Server.

To make an AssetBundle, first, we need to build the asset of unity project.You can manually mark an asset as AssetBundle. Select any asset from project view and in the bottom, part from model view give AssetBundle name. You can also set version of AssetBundle.

In this tutorial, we will load an asset that contains sound files. The project contains a SoundAsset prefab inside Assets folder. We will use this to create an AssetBundle, Load it into the project and play the sound.

Create a script “ExportAssetBundles” inside Editor folder and add following lines of code in it:

Step-2: Load AssetBundle From The Server

We will load the SoundAsset from the server and play the Audio clip attached to it.
Create a new script name it “AssetBundleSample ”. It will manage all the loading operation of asset bundle. Add the following lines code in it:

Attach this script to an empty game object. create a Text to display loading status and assign to this script. Also, give the server URL of AssetBundle.

Now create 3 buttons and add LoadSoundFiles() method in the OnClick listener. Pass “Music1” as a parameter in button1, ”Music2” as a parameter in button2 and ”Music2” and “Music3 as a parameter in button3”. Now hit play. The loading will display as “%” format. when done with loading, it will first instantiate the AssetBundle and then will play the Audio Clip attached to it.

#2

How To Get Data From AssetBundle In Local?

In this example, we will create a scene as Asset bundle and load it from local memory. Create a scene and name it “AssetScene”. Add some images and other game objects to this scene you want.

Step-1: Build A Scene As AssetBundle

To create a scene as asset bundle, update the following lines of code to the “ExportAssetBundles” script:

This script will allow us to create an asset bundle from the scene. Right click on AssetScene in project view and click Build Scene Bundle. It will create an asset Bundle inside Assets/AssetBundle folder.

Step-2: Load AssetBundle From The Local

To load any files from local memory in any platform, first, we need to save it somewhere in the local machine. Upload this AssetScene to the server.

Update the AssetBundleSample Script with following lines of code:

Create a button name it Loads scene and adds LoadFromLocal() method in the OnClick listener. LoadSceneBundle will load the Scene Asset from the server and store it in the local memory of the device. Call this coroutine in Start() method. Now when you click on LoadScene button, you will be navigated to the SceneBundle. You do not need to add this scene to build setting.

Full Source Code

Download APK

 

 

Animation Clips are one of the core elements of Unity’s animation system. Gameobjects in Unity can be animated using animation Clip. Using Animation Clips, we can change position, rotation and scale of a gameobject as well as other properties like float or integer value of material from the script.

 

In Unity, Animation clips are created using “Animation“ window, under Windows from the main menu -> Animation. You can see an animation window like the game view and scene view in unity editor :

 

 

All animations in Unity are handled by the Animation Controller Component. This is the component that references to 1 or more animation clips to animate a gameobject.

 

To create an animation clip to scale a gameobject we need to modify the scale property of that gameobject. Create a Cube. Select that Cube in the hierarchy, and you can see there is a button in Animation window “Create” to create a new animation clip :

 

 

By pressing the create button in the Animation view, a new animation clip is created. Unity will ask to pick the name & location to save this new Animation Clip. Unity also creates an Animator Controller asset with the same name as the selected GameObject, adds an Animator component to the GameObject, and connects the assets up appropriately. Give the name Scale and save the clip. Unity will automatically create an animation clip named “Scale_Anim“ and an animator controller named Cube. It is the name of the currently selected gameobject. The animation clip contains the .anim extension. Now can see animation window with frames.

 

 

Now select the gameobject Cube and click on “ Add Property ” button.Select scale form Transform and press “ + ” button to create scale a property. The upper part number defines the playback duration and time line of the animation clip.You can click anywhere on the Animation window time line to move the playback head to that frame, and preview or modify that frame in the Animation Clip. The numbers in the time line are shown as seconds and frames, so 1:30 means 1 second and 30 frames.Default keyframes are created from 0:00 to 1:00. We can add new keyframes by right clicking in the animation window, you will see a menu and then select Add key. You can drag this key anywhere in the animation window to create an animation clip.

 

There are 2 modes of animation window to create an animation clip :

Dopesheet - Dopesheet allows us to create animation clips with some exact specific values for the gameobject.

Curves - Curves allows us to create animation clips with curves value.

When you are creating an animation clip, the animation window will record the animation. The red button at the top left corner is highlighted. In this tutorial, we will use Dopesheet to create an animation clip.

We will scale cube from (1,1,1) to (2,2,2) using animation clip. Select the first keyframe at 0:00 and then select cube from hierarchy and set scale value from Transform component to (1,1,1). This will set the value of cube scale to (1,1,1) at the beginning of the clip. Now add a new keyframe at the time 0:50 and set the scale value of cube to (2,2,2). This will set the cube scale to (2,2,2) in the middle of the animation. Now select the last keyframe and set the scale to again (1,1,1). This will set the value at last of animation.

 

Now hit the play in the animation window to preview the animation. You can see scale animation of the cube. This animation clip is assigned as a default state in the animation controller.

 

At last, you will see the keyframes value.

 

 

Now hit the play button in Unity editor. You will see a Zoom-in Zoom-out animation for the cube.

 

Now create another clip for rotation.Click in the dropdown of animation list below the record button and then select Create New Clip. Save clip as Rotate_Anim.

 

 

We will rotate Cube from 0 to 360 degrees. Select the first keyframe at 0:00 and then select cube from hierarchy and set rotation value from Transform component to (0,0,0). This will set the value of cube rotation to (0,0,0) at the beginning of the clip. Now add a new keyframe at the time 0:50 and set the rotation value of cube to (0,0,180). This will set the cube rotation to (0,0,180) at the middle of the animation. Now select the last keyframe and set the rotation to (0,0,360). This will set the value at last of animation.

 

Now hit the play in the animation window to preview the animation. You can see rotation animation of cube. This animation clip is assigned as a new animation state in the animation controller.

 

To play both animations for the cube, we need to make a transition between these 2 clips. Add a transition from Scale_anim to Rotate_Anim in animator controller. To make a transition right click on the current state and select Make Transition and then drag towards the destination state and then release. You will see a transition with arrow line. Now add a transition from Rotate_Clip to Scale_Clip. 

 

 

Now hit the play button in the Unity Editor. You will see the scale animation and rotation animation of the cube.

Final output:

Full Source Code

Download APK

 

Photon is a real-time multiplayer mobile game development agency framework which has server and cloud services. Games are hosted in globally distributed Photon Cloud so we do not need to worry about hosting. All the operations related to multiplayer functionality are handled by Photon. Free version of PUN comes with 20 CCU subscription but you can upgrade it as you need it. More info can be found on their official site (https://www.photonengine.com/en-US/Photon).

In this tutorial we are going to cover in detail the integration of Photon Unity Networking (PUN) with Unity.

Prerequisites

1. Unity 5 or later , you can download from here (https://unity3d.com/)

2. Photon Unity Networking (PUN) package, you can download free package from Asset store from here (https://www.assetstore.unity3d.com/en/#!/content/1786)

 

In this tutorial we are using Unity 5.6.3.

We have following major things in this blog:

1. Setup photon in Unity project

2. Manage connection with photon

3. Synchronization of players

4. Testing

#1 Configure Game on Photon:

Step-1: Login to your account

We need an account on Photon to configure our game .Go to https://www.photonengine.com/en-US/Photon. Log in to your account if you already have otherwise you can click on Register to create new account.

 

Step-2 : Create and configure game

Provide necessary info related to your game and create game.

 

Step-3 : Get your App Id from dashboard

We have completed the setup on Photon side.

 

#2 Setup Photon in Unity Project:

Before we start, download the PUN package from the Asset store (https://www.assetstore.unity3d.com/en/#!/content/1786). After download, open PUN settings to configure the project for photon. You can find PUN settings under window -> Photon Unity Networking -> Highlight server settings.

 

It will open a setting wizard that allows to configure project with photon. Add your app in in the App id field of PhotonServerSettings. Set your appropriate region. Check AutoJoinLobby so photon will automatically join the player in the lobby.

 

#3 Manage connection with Photon:

Create a new script called PUNManager and add it to an empty gameobject. Using this script we will handle all server side operations. First of all, we need to connect with Photon. This will enable us to host and join rooms of our game based on the AppID. Call PhotonNetwork.ConnectUsingSettings () method to make connection. We can pass the version number of the game as a string in the argument. Import namespace Photon.

Use following code to connect your project with photon:

To check status whether we are connected or not, take a Text and show status using PhotonNetwork. connectionStateDetailed. After connection, photon will automatically join player in a lobby. When a player joins a lobby OnJoinedLobby callback is called. After that we need to connect to a room. PhotonNetwork.JoinRandomRoom  is allow to connect players in a random available room. If no room available it will create a new one. We can also use PhotonNetwork.JoinRoom to connect if we already know the room name. In this tutorial we will use JoinRandomRoom. We will create a button that will handle this event. Add the JoinRoom method in buttons click event in Inspector. After connecting to a room, the function OnJoinedRoom is called.

Add following code to PUNManager script:

#4 Synchronization:

As now we are able to connect multiple players, add some game coding to the project. Create a plane and position it with (0, -0.5f, 0). Create a cube, name it Player. Create a script PlayerManager that will handle players’ movements.

Add following code in PlayerManager:

Add this script to Player gameobject. Next, add the Photon view component to the player object (Component > Photon View).

This will enable us to send data packages over the network to synchronize the player. Observe option is automatically set to “reliable delta compressed”. This means that synchronized data will be sent automatically, but only if its value changed. So for example if you move as a player, your position will be updated on the server. By setting it to “off” there is no automatic synchronization at all and you have to do it manually. Drag the transform component into the observe field. At the bottom the field serialization is now added, where you can state which values from the transform to synchronize. Set its value to “Only Position”. In the project hierarchy, create a folder called “Resources”. In it, place the player object from the scene to make it a prefab. This is needed to instantiate the object on the network. In the PUNManager-script add a public game object variable for the player prefab. When OnJoinedRoom() is called, we want to create the player object. Instantiating on a Photon network requires the prefab to be placed in the Resources folder. The prefab parameter for PhotonNetwork.Instantiate() is a string instead of a gameobject. Also create a gameobject plane that can be activated when connected to room.

Make a build of project and run both. You can control both players movement, not just your own. To Solve this issue, we need to check is this a player that instantiated the object? To implement this , we need to check whether the object on the network “is mine” in the player script. Add using Photon; namespace and replace Monobehaviour class with PunBehaviour:

Again make a build and test your code. You can now control only one of the players.

#5 State Synchronization:

For synchronization between players, photon provides a callback OnPhotonSerializeView() that constantly updates values over the network. This method is useful to sync data that changes often like player movement.

Go to the Photon view component on the player’s prefab. The observed field contains the component that will be synchronized. Drag the component of the player script in the observed field.

Add OnPhotonSerializeView() to the player script.

This function is automatically called every time it either sends or receives data. If the user is the one updating the object, he/she writes to the stream. The clients receive the stream and can apply the data that was send to the object. In the example below, the user sends the transform’s position with stream.SendNext() and this is received by the clients with stream.ReceiveNext(). The order of data send should be the same as the order in which the data is received, otherwise values will get mixed up.

#6 Remote Procedure Calls:

Another method of network communication is Remote Procedure Calls (RPCs), which is more useful for data that does not constantly change. This approach is only able to send integers, floats, strings, vectors and quaternions. An RPC is sent by calling photonView.RPC(), in which you define the function name and parameters. Also the Photon targets are required: “PhotonTargets.All” sends the data to all players including local player, “PhotonTargets.OthersBuffered” to everyone in the room except yourself. Add [PunRPC] before function implementation to make it a Remote Procedure Call. To implement this we will create a method that will change the color of the material of your own player.

Add following lines of code in PlayerManager script:

To send data to all the players in room we need to use PhotonTargets. All as target when calling a RPC method. Take a Text and a button that will handle this event. Add PhotonView component to the PUNManager gameobject from Component > Photon View. Create a method named ChangeTextColor and add it in button click event in inspector.

Add the following code in the PUNManager script:

Make a new build and test it. Click on the “Change color ” button and you can see that the text color is changing on both side.

#7 Testing

Now create another build and if you run the game, you will see the player’s colors can be changed. Also you can see the other player’s movement.

You can find the complete code here:

Full Source Code

Download APK