Unity script (4)

Video tutorial: https://www.bilibili.com/video/BV12s411g7gU?p=149 

Table of contents

keyboard input 

InputManager 


keyboard input 

Returns true when the key specified by name is held down by the user:

bool result=Input.GetKey(KeyCode.A);

Returns true for the frame when the user presses the key with the specified name:

bool result=Input. GetKeyDown(KeyCode.A);

Returns true the frame the user releases the key with the given name:

bool result=Input. GetKeyUp(KeyCode.A); 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputDemo: MonoBehaviour
{
    public bool isDown;

    void Update()
    {
        isDown = Input.GetMouseButton(0); 

        //检测按下C键同时按下D键
        if (Input.GetKey(KeyCode.C) && Input.GetKey(KeyCode.D))
        {
            print("同时按下C、D键");
        }
    }
}

ps: KeyCode is essentially an enumeration class 

Sight

Through the right mouse button, the camera lens can be zoomed

Mount the following script to the automatically created Main Camera in the template scene (if not, create an empty object with a Camera component), and set the Field of View property of the Camera component to 60 as the default value when not zoomed 

  

1. Single zoom

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraZoom : MonoBehaviour
{
    new private Camera camera;
    private bool isZoom = false;

    void Start()
    {
        camera = GetComponent<Camera>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            isZoom = !isZoom;
        }
        if (isZoom)
        {
            camera.fieldOfView=Mathf.Lerp(camera.fieldOfView,20,0.1f);
            if(Mathf.Abs(camera.fieldOfView-20)<0.1f)
                camera.fieldOfView=20;
        }
        else
        {
            camera.fieldOfView=Mathf.Lerp(camera.fieldOfView,60,0.1f);
            if(Mathf.Abs(camera.fieldOfView-60)<0.1f)
                camera.fieldOfView=60;
        }
    }
}

Unscaled: 

After scaling:

 2. Multiple zoom

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraZoom : MonoBehaviour
{
    new private Camera camera;
    public float[] zoomLevel;
    private int index = 0;

    void Start()
    {
        camera = GetComponent<Camera>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            // index = index < zoomLevel.Length - 1 ? ++index : 0;
            index = (++index) % zoomLevel.Length;
        }
        camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoomLevel[index], 0.1f);
        if (Mathf.Abs(camera.fieldOfView - zoomLevel[index]) < 0.1f)
            camera.fieldOfView = zoomLevel[index];
    }
}

Mathf.Lerp(float a, float b, float t) : Linear interpolation between a and b by t

a starting point
b end point
t interpolation between two floating point numbers

When t=0, return a; when t=1, return b; when t=0.5, return the midpoint of a and b 

in the code 

index = index < zoomLevel.Length - 1 ? ++index : 0;

 Equivalent to

index = (++index) % zoomLevel.Length;

But the latter uses the idea of ​​a circular queue in the data structure

parameter settings 

 

 Unscaled: 

Level 1 zoom:

2 levels of zoom

InputManager 

That is, the input manager, located in Edit-->Project Settings-->InputManager. By using the virtual axis name to get the input of the custom key in the script, the player can modify the virtual axis according to personal preference when the game starts 

Descriptive Name : In the game loading interface, the detailed description of the forward button

Descriptive Negative Name : In the game loading interface, the detailed description of the reverse button

Negative Button : This button will send a negative value to the axis

Positive Button : This button sends a positive value to the axis

Alt Negative Button : Another button that sends negative values ​​to the axis

Alt Positive Button : Another button that sends positive values ​​to the axis 

ps: A virtual axis can bind up to four keys, if more than four, you can create a new virtual axis with the same name to bind the rest of the keys

Gravity : the speed at which the input resets, only for keys of type key/mouse

Dead : Any input value (regardless of positive or negative value) less than this value will be regarded as 0, used for joystick

Sensitivity : Sensitivity, for keyboard input, the larger the value, the faster the response time, and the smaller the value, the smoother it is. For mouse input, setting this value will scale proportionally to the actual moving distance of the mouse

Snap : If this setting is enabled, when the axis receives a reverse input signal, the value of the axis will be set to 0 immediately, otherwise the reverse signal value will be applied slowly. For key/mouse input only

Invert : Enabling this parameter allows the forward button to send a negative value, and the reverse button to send a positive value 

Type

1. Key /Mouse : key/mouse

2. Mouse Movement : mouse movement and scroll wheel

3. Joystick Axis : Joystick

Axis : The input axis of the device (joystick, mouse, gamepad, etc.)

Joy Num : Set which joystick to use, the default is to receive input from all joysticks. For input shafts and non-keys only

get virtual axis

 The following codes all return Boolean, so it can only judge whether the key bound to the virtual button is pressed

bool result=Input.GetButton("虚拟按钮名");

bool result=Input.GetButtonDown("虚拟按钮名");

bool result=Input. GetButtonUp("虚拟按钮名");

 The following codes all return floating-point type, so it can judge the positive or negative of the virtual axis (-1~1), or the positive or negative represented by the key on the virtual axis

float value=Input.GetAxis ("虚拟轴名"); 

float value=Input.GetAxisRaw("虚拟轴名");

camera rotation

Move the mouse vertically to rotate the camera up and down, and move the mouse horizontally to rotate the camera left and right

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//控制摄像机随鼠标的移动而旋转
public class DoRotation : MonoBehaviour
{
    public float hor;
    public float rotateSpeed;

    private void FixedUpdate()
    {
        //鼠标移动
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");

        if (x != 0 || y != 0)
            RotateView(x, y);
    }

    private void RotateView(float x, float y)
    {
        x *= rotateSpeed;
        y *= rotateSpeed;


        this.transform.Rotate(-y, 0, 0);
        this.transform.Rotate(0, x, 0, Space.World);
    }
}

player moves 

Keyboard vertical input makes the aircraft move forward and backward, keyboard horizontal input makes the aircraft move left and right

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 10;

    private void Update()
    {
        float hor = Input.GetAxis("Horizontal");
        float ver = Input.GetAxis("Vertical");

        if (hor != 0 || ver != 0)
            Movement(hor, ver);
    }

    private void Movement(float hor, float ver)
    {
        hor *= moveSpeed * Time.deltaTime;
        ver *= moveSpeed * Time.deltaTime;

        transform.Translate(hor, 0, ver);
    }
}

Guess you like

Origin blog.csdn.net/qq_53401568/article/details/128535085