Unity study notes- API 02

Time

Provides an interface for obtaining time information from Unity.

Time.deltaTime   : The time taken to complete the previous frame (read-only). The incremental time changes in real time to ensure that the transformation is stable when the frame rate changes.

void Update()
{
    transform.Rotate(0,0,speed*Time.deltaTime);    
}

Time.fixedTime : The time when the last FixedUpdate was started. The time since the game started.

Time.time : The time the application has been running (read-only). Scale the image by time.

Time.timeScale : The scale of time elapsed. Can be used for slow motion effect. 

Time.unscaledTime : Time independent of timeScale (read-only). The time since the game started.

Time.realtimeSinceStartup : Real time since the game started (read-only).

Prefab

Prefab - An asset type, a reusable game object stored in the project view. It can be placed in multiple scenes, or multiple in the same scene. When modifying the prefab, all prefabs that have been placed in the scene are also modified.

create prefab

Create a Prefab folder under the Assets folder , and drag the created game objects in the scene into this folder.

Disconnect the object from the prefab

Select the game object, right-click and select Prefab -> Unpack (disconnect the current layer from the prefab) / Unpack Completely (disconnect the current layer from all sublayers from the prefab).

Input

 Interface to access the input system

Input.GetKeyDown(name) : Returns true during the frame when the key identified by name is pressed . (the frame that was pressed)

Input.GetKeyUp(name) : Returns true during the frame when the key identified by name is released . (the frame released)

void Update()
{
    if(Input.GetKeyDown(KeyCode_Space))
    {
        Debug.Log("按下空格");
    }
    if(Input.GetKeyUp(KeyCode_Space))
    {
        Debug.Log("释放空格");
    }
}

Input.GetKey(Key) : Returns true when the user presses the key identified by name. (Report the state of the pointing key, press and hold to return true)

Input.GetMouseButton(mouseButton) : Returns whether the specified mouse button is pressed. 0: left button; 1: right button; 2: middle button.

Input.GetMouseButtonDown(mouseButton) : The frame period during which the user pressed the mouse button .

Input.GetMouseButtonUp(mouseButton) : The frame during which the user releases the mouse button .

void Update()
{
    if(Input.GetMouseButton(0))
    {    
        Debug.Log("按住鼠标左键");
    }
    if(Input.GetMouseButtonDown(0))
    {
        Debug.Log("按下鼠标左键");
    }
    if(Input.GetMouseButtonUp(0))
    {
        Debug.Log("释放鼠标左键");
    }
}

virtual axis

By default, each project created has several input axes created. These axes allow immediate use of keyboard, mouse, and joystick input in projects.

view virtual axis 

Edit -> Project Settings -> Input Manager

get/use virtual axis

Input.GetAxis(axisName) : Returns the value (float) of the virtual axis identified by axisName. The range of this value is between -1~1, -1 means left, 1 means right, and 0 means middle.

Input.GetAxisRaw(axisName) : Same as GetAxis, no smooth transition is applied (you can implement it yourself), the return type is float, but the returned values ​​​​are only -1, 0 and 1.

public Rigidbody rigid;

void FixedUpdate()
{
    Movement();
}

private void Movement()
{
    float horizontalMove = Input.GetAxis("Horizontal");
    //float horizontalMove = Input.GetAxisRaw("Horizontal");
    rigid.velocity = new Vector3(horizontalmove*speed,rigid.velocity.y,rigid.velocity.z);
}

Guess you like

Origin blog.csdn.net/hanshuangzirui/article/details/125859566