Skip to main content
Unity3D

Unity3D Scripting API #4 : Update() ☑️

By January 24, 2021February 14th, 2021No Comments
Unity3D Scripting API

Update():

☑️ This method is called continuously every frame
☑️ The Update() method is good for receiving inputs from the user continuously. Anything that needs to change or be adjusted regularly will be written inside the Update() method for example⏱️ Timers.
☑️ Also used for non-physics based movement for example non-physics based character controller in 2D games
☑️ Update() depends on the game's refresh rate and the intervals are NOT regular. So if one frame takes longer to process, then the next Update() call is delayed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Hello");
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Updating");
        ///move cube 1 unit to the left per second
this.transform.position = this.transform.position + new Vector3(-1f, 0f, 0f) * Time.deltaTime;

    }
}
Note:
The transform property stores the position, rotation and scale of a game object to which a script is attached in the form of a Vector3 structure. It allows us to access the various properties and methods of the Transform class.

Vector3: Vector3 has a 3D direction, like a x, y and z point in a 3D space

Time.deltaTime allows you to move the object a n units per second rather than per frame (i.e. it slows it down – try the code without it and see

Full list here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

Leave a Reply