Looking For Anything Specific?

List of basic scripts for beginners in unity

This time I will only provide basic tips in the form of a list of basic scripts that can make it easier for you game developers, especially Unity developers, in working on existing projects.

 

Unity is the most popular game engine with a variety of products that can be produced from this game engine, such as 2d and 3d games, AR, VR, film materials, not to mention that Unity can support various types of platforms such as Android, iOS, PC, etc. Also has a lot of ready-made assets in the Unity Asset Store.

 

But in my opinion the main strength of Unity lies in programming using the C# programming language, you can use it to handle user input, manipulate game objects in the scene, detect interactions between objects, create new GameObjects etc. Which can help you in letting go of the logic of the game you are thinking about

 

Regarding programming, this is what sometimes becomes an obstacle for those of us who have good ideas but can't realize them, maybe easy problems can be solved by searching on google, but this process will cause a lot of work to be delayed and stop the pace of our work.

 

That's why I made a list of these basic scripts to make it easier for all of you, I suggest memorizing them, not just reading and then solving the problem.

 

Let's start with the first

 

  • Event initial execution code

In a script, this unity code code is usually called earlier than other codes. For other information, please click the link below

Unity - ExecutionOrder

You just need to put additional code between { code here }

private void Awake() { /* Called while script is loading */ }

private void OnEnable() { /* Called every time the object fires */ }

private void Start() { /* Called on first frame when script is running */ }

private void Update() { /* Called once per frame */ }

private void LateUpdate() { /* Calls every frame after Update */ }

private void OnBecameVisible() { /* Called when renderer is visible to any Camera */ }

private void OnBecameInvisible() { /* Called when the renderer is no longer visible to any Camera */ }

private void OnDrawGizmos() { /* Allows you to draw Gizmos(lines) that appear in Scene View instead of game view */ }

private void OnGUI() { /* Called multiple times per frame in response to GUI events */ }

private void OnApplicationPause() { /* Called at end of frame when pause is detected */ }

private void OnDisable() { /* Called every time the object is disabled */ }

private void OnDestroy() { /* Called if previously active Game Objects have been destroyed */ }

Next, this code is slightly different, this code will be called every fixed time step or exact time step which can be seen Edit > Project Settings > Time > Fixed Timestep

private void FixedUpdate() { /* called every fixed timestep */ }

 

 

 

  • code manipulating Game Objects

/* GameObject creation*/

Instantiate(GameObject prefab);

Instantiate(GameObject prefab, Transform parent);

Instantiate(GameObject prefab, Vector3 position, Quaternion rotation);

 

/* Example */

Instantiate(bullet);

Instantiate(bullet, bulletSpawn.transform);

Instantiate(bullet, Vector3.zero, Quaternion.identity);

Instantiate(bullet, new Vector3(0, 0, 10), bullet.transform.rotation);

 

/* To destroy GameObject */

Destroy(gameObject);

 

/* Search GameObject */

GameObject myObj = GameObject.Find("Name IN Hierarchy");

GameObject myObj = GameObject.FindWithTag("TAG");

 

/* Accessing Components in the game object */

Example myComponent = GetComponent<Example>();

AudioSource audioSource = GetComponent<AudioSource>();

Rigidbody rgbd = GetComponent<Rigidbody>();

  • Code set object vector

X = Right/Left Y = Up/Down Z = Forward/Back (X,Y,Z)

Vector3.right /* (1, 0, 0) moves right 1X */                                     Vector2.right /* (1, 0) */

Vector3.left /* (-1, 0, 0) move left -1X */                                         Vector2.left /* (-1, 0) */

Vector3.up /* (0, 1, 0) moves up 1Y*/                                              Vector2.up /* (0, 1) */

Vector3.down /* (0, -1, 0)move down -1Y */                                  Vector2.down /* (0, -1) */

Vector3.forward /* (0, 0, 1) moves forward 1Z*/

Vector3.back /* (0, 0, -1)move back -1Z */

Vector3.zero /* (0, 0, 0) */                                                                  Vector2.zero /* (0, 0) */

Vector3.one /* (1, 1, 1) moves right, up, forward*/                       Vector2.one /* (1, 1) */

float length = myVector.magnitude /* Length of this vector */

myVector.normalized /* Keeps direction, but reduces length to 1 */

  • Code Set Time

/* Time in seconds since the game started */

float timeSinceStartOfGame = Time.time;

 

/* The scale on which time elapses */

float currentTimeScale = Time.timeScale;

/* set time in game 0 = stop game not running */

Time.timeScale = 0;

 

/* Time in seconds required to complete the last frame */

/* use with Update() or LateUpdate() */

float timePassedSinceLastFrame = Time.deltaTime;

 

/* interval in seconds when physics and frame rate are updated*/

/* Use with FixedUpdate() */

float physicsInterval = Time.fixedDeltaTime;

  • Code that governs physics

This code regulates how objects react with other objects in the game scene according to the original physics theory

private void OnCollisionEnter(other object variable) { Debug.Log(gameObject.name + " react " + hit.gameObject.name); }

private void OnCollisionStay(other object variable) { Debug.Log(gameObject.name + " continues to react" + hit.gameObject.name); }

private void OnCollisionExit(other object variable) { Debug.Log(gameObject.name + " reaction completed " + hit.gameObject.name); }

 

/* Trigger must be checked on one of the objects */

private void OnTriggerEnter(other object variable) { Debug.Log(gameObject.name + " react " + hit.name); }

private void OnTriggerStay(other object variable) { Debug.Log(gameObject.name + " continues to react " + hit.name); }

private void OnTriggerExit(other object variable) { Debug.Log(gameObject.name + " reaction

 

  • Code that governs Coroutines or recurring things

/* Create Coroutines */

private IEnumerator CountSeconds(int count = 10)

{

  for (int i = 0; i <= count; i++) {

    Debug.Log(i + " second(s) have passed");

    yield return new WaitForSeconds(1.0f);

  }

}

 

/* call coroutines */

StartCoroutine(CountSeconds());

StartCoroutine(CountSeconds(10));

 

/* call Coroutines that may need to be terminated */

StartCoroutine("CountSeconds");

StartCoroutine("CountSeconds", 10);

 

/* Stop Coroutine */

StopCoroutine("CountSeconds");

StopAllCoroutines();

 

/* Save and call Coroutine from variable */

private IEnumerator countSecondsCoroutine;

 

countSecondsCoroutine = CountSeconds();

StartCoroutine(countSecondsCoroutine);

 

/* Terminate saved Coroutine */

StopCoroutine(countSecondsCoroutine);

 

/* Type of data that Coroutine returns */

yield returns null; // Wait until next Update() call

yield return new WaitForFixedUpdate(); // Wait until the next FixedUpdate() call

yield return new WaitForEndOfFrame(); // Wait until all these frames are executed

yield return new WaitForSeconds(float seconds); // Wait for game time in seconds

yield return new WaitUntil(() => MY_CONDITION); // Wait until the special condition is met

yield return new WWW("MY/WEB/REQUEST"); // Waiting for web request

yield return StartCoroutine("MY_COROUTINE"); // Wait for another Coroutine to finish

  • The last one is the input code from the player

if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Space key pressed"); }

if (Input.GetKeyUp(KeyCode.W)) { Debug.Log("release key W"); }

if (Input.GetKey(KeyCode.UpArrow)) { Debug.Log("up arrow key pressed"); }

 

/* Input button is located in Edit >> Project Settings >> Input */

if (Input.GetButtonDown("ButtonName")) { Debug.Log("Button pressed"); }

if (Input.GetButtonUp("ButtonName")) { Debug.Log("Button released"); }

if (Input.GetButton("ButtonName")) { Debug.Log("Button is being pressed"); }

Alright, that's the basic code that we can use directly in our projects according to their respective uses

 

Until here the tips this time may be useful, if you have any questions please comment below, thank you for visiting, see you in the next article

Posting Komentar

0 Komentar