How to Create Your Own 3D World in Unity (Example)

If you wish to become a creator of 2D and 3D games, you should know how to create worlds and place objects. In this example our expert will tell you how to create your own 3D world using Unity software. Here you can read scripts for doors, the rotation of objects, and for an audio player for sounds when the action is complete.

Many students are assigned to write a 3D videogame during programming classes related to computer graphics. If you have no time for working on a computer graphics assignment or simply have something more important to do, you can use the services of AssignmentShark. We know how some technical disciplines are difficult to understand, and such assignments that ask you to create your own 3D world can turn your life into a disaster. Our experts are able to finish any project, even if the deadline is tomorrow. If you are aiming to be on time with assignments in every discipline and maintain high grades, you should use our services immediately! Our experts strictly follow all deadlines and due dates, as we know how important it is for our clients. If you stay unsatisfied with the completed work, you can always ask for correction. You will pay only for work that adheres to your requirements. We are available 24/7 – no need to wait to make an order!

Aims

  • Develop a 3D interactive world.

Learning Outcomes

  • Design and create a simple 3D world.
  •  Populate it with objects.

 

Create a 3D World

 

First we need to create the room – the model and textures. Download some free textures using Google and create the model. To do it, the first thing we need to is to create a cube:

Then, resize it to look like a wall:

Duplicate it several times and create some kind of building:

Add the texture images:

Then, we should set up the sources of light using the light component:

Then, we add the first person controller to be able to explore the world:

Add the door and write a script for it:

Door.cs:

using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour {
	// how smooth the door opens
	float smoothness = 2.0f;
	private bool open;

	// rotation of closed door
	private Vector3 closeRot;
	// rotation of opened door
	private Vector3 openRot;
	// pivot of door rotation
	private Transform pivot;

	void Start(){
		// get closed and opened door rotaions
		closeRot = transform.eulerAngles;
		openRot = new Vector3 (closeRot.x, closeRot.y + 90.0f, closeRot.z);
		// pivot of rotaion is the only child
		pivot = transform.GetChild (0);
	}

	void Update (){
		if(open){
			//Open door
			pivot.eulerAngles = Vector3.Slerp(pivot.eulerAngles, openRot, Time.deltaTime * smoothness);
		}else{
			//Close door
			pivot.eulerAngles = Vector3.Slerp(pivot.eulerAngles, closeRot, Time.deltaTime * smoothness);
		}
	}

	// activates when collider enters the trigger
	void OnTriggerEnter (Collider other){
		// when the player is near the door - open it
		if (other.gameObject.tag == "Player") {
			open = true;
		}
	}

	// activates when collider exits the trigger
	void OnTriggerExit (Collider other){
		// when the player is far from the door - close it
		if (other.gameObject.tag == "Player") {
			open = false;
		}
	}
}

Download and add some showpieces and pictures, and add them to our building:

Write a script for rotation:

 

using UnityEngine;

using System.Collections;


public class Showpiece : MonoBehaviour {

        // time of showpiece full spin in seconds

        public float spinTime = 10f;


        // Use this for initialization

        void Start () {

        

        }

        

        // Update is called once per frame

        void Update () {

                // spinning showpiece in time

                transform.Rotate(0, Time.deltaTime * 360 / spinTime, 0, Space.World);

        }

}

And now, we will add the capability to examine the showpiece using the “E” button:

 

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using UnityStandardAssets.Characters.FirstPerson;


public class ShowpieceExplorer : MonoBehaviour {

        // mask to filter layers for raycasting

        public LayerMask mask;

        // ui text for showing "Press E to explore"

        public Text notifyText;

        // image for showpiece description

        public GameObject exploreImage;

        // flag to show if player is exploring some showpiece

        private bool isExploring = false;

        

        // Update is called once per frame

        void Update () {

                // clear text that tells to press E key for exploring

                notifyText.text = "";        

                // if not exploring any showpiece

                if (!isExploring) {                                

                        RaycastHit hit;

                        // casting a ray from the main camera position on 2 units to check if something is in front of the player

                        if (Physics.Raycast (Camera.main.transform.position, Camera.main.transform.forward, out hit, 2f, mask)) {

                                // check if player is looking at showpiece

                                if (hit.collider.tag == "Showpiece") {

                                        // display text that tells to press E key for exploring

                                        notifyText.text = "Press E to explore";

                                        // if E key is pressed

                                        if (Input.GetKeyDown (KeyCode.E)) {

                                                // set exploring state true

                                                isExploring = true;

                                                // get showpiece name

                                                string name = hit.collider.name;

                                                // get descriptive text from the file

                                                string text = System.IO.File.ReadAllText(@"ShowpieceDescription\" + name + ".txt");

                                                // set descriptive text and show it

                                                exploreImage.GetComponentInChildren<Text>().text = text;                                                        

                                                exploreImage.SetActive(true);                                                                                                                

                                                // freeze player

                                                GetComponent<FirstPersonController> ().enabled = false;                

                                        }

                                }

                        }

                // if player is exploring some showpiece

                } else {

                        // if E key is pressed while exploring

                        if (Input.GetKeyDown (KeyCode.E)) {

                                // set exploring state false

                                isExploring = false;

                                // hide descriptive text

                                exploreImage.SetActive(false);

                                // release player

                                GetComponent<FirstPersonController> ().enabled = true;

                        }

                }

        }

}

Then we add the coin element and put it to each showpiece:

Write a script for all of them to rotate and be collectable:

 


using UnityEngine;

using System.Collections;

public class Stamp : MonoBehaviour {

        // stamp collector script

        private StampCollector collector;

        // time of stamp full spin in seconds

        public float spinTime = 2f;

        // Use this for initialization

        void Start () {

                // find stamp collector script in the scene

                collector = GameObject.FindGameObjectWithTag ("GameController").GetComponent<StampCollector> ();

        }

        

        // Update is called once per frame

        void Update () {

                // spinning and moving stamp in time

                transform.Rotate(0, Time.deltaTime * 360 / spinTime, 0, Space.World);

                transform.localPosition = new Vector3 (0, 0, 0.2f * Mathf.Sin(Mathf.PI * 2 * Time.time / spinTime));

        }

        // OnTriggerEnter is called when some collider hits the stamp trigger

        void OnTriggerEnter(Collider other)

        {

                // if this collider is player

                if (other.tag == "Player") {

                        // add one more collected stamp

                        collector.Add ();

                        // delete stamp from scene

                        Destroy (transform.parent.gameObject);

                }

        }

}

And the script which shows that all the coins are collected:

using UnityEngine;

using System.Collections;

using UnityEngine.UI;


[RequireComponent(typeof(AudioSource))]

public class StampCollector : MonoBehaviour {

        // current number of collected stamps

        private int stamps = 0;

        // total number of stamps in the scene

        public int maxStamps = 10;

        // text for displaying number of collected stamps

        public Text stampText;


        // stamp take sound

        public AudioClip stampTakeAudio;


        // Use this for initialization

        void Start () {

                UpdateStampText ();

        }


        // add one stamp to collected

        public void Add ()

        {

                stamps++;

                // play sound

                GetComponent<AudioSource>().PlayOneShot(stampTakeAudio);


                // if all stamps are collected

                if (stamps == maxStamps) {

                        // display final message

                        stampText.text = "Exploring complited";

                } else {

                        // update displayed number of collected stamps

                        UpdateStampText ();

                }

        }


        // update collected stamps count in the ui

        private void UpdateStampText()

        {

                stampText.text = "Stamps: " + stamps + "/" + maxStamps;

        }

}

Also, this script contains the AudioSource component to play the sound when the stamp is completed.

Here is how our 3D interactive world looks:

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.