Unity脚本(四)

视频教程:https://www.bilibili.com/video/BV12s411g7gU?p=149 

目录

键盘输入 

InputManager 


键盘输入 

当通过名称指定的按键被用户按住时返回true:

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

当用户按下指定名称按键时的那一帧返回true:

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

在用户释放给定名称按键的那一帧返回true:

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本质上是一个枚举类 

瞄准镜

通过鼠标右键,实现摄像机镜头缩放

将以下脚本挂载至模板场景中自动创建的Main Camera(如没有就创建一个带有Camera组件的空物体),将Camera组件的Field of View属性设置为60,作为未缩放时的默认值 

  

1.单倍缩放

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;
        }
    }
}

未缩放: 

缩放后:

 2.多倍缩放

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):在a与b之间按t进行线性插值

a 起点值
b 终点值
t 两个浮点数之间的插值

当t=0时,返回a;当t=1时,返回b;当t=0.5时,返回a和b的中点 

代码中的 

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

 等同于

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

但后者采用的是数据结构中循环队列的思想

参数设置 

 

 未缩放: 

1级缩放:

2级缩放

InputManager 

即输入管理器,位于Edit-->Project Settings-->InputManager。在脚本中通过使用虚拟轴名称获取自定义键的输入,使玩家可以在游戏启动时根据个人喜好对虚拟轴进行修改 

Descriptive Name:游戏加载界面中,正向按键的详细描述

Descriptive Negative Name:游戏加载界面中,反向按键的详细描述

Negative Button:该按钮会给轴发送一个负值

Positive Button:该按钮会给轴发送一个正值

Alt Negative Button:给轴发送负值的另一个按钮

Alt Positive Button:给轴发送正值的另一个按钮 

ps:一条虚拟轴最多可以绑定四个键,若超过四个,可以在新建同名的虚拟在绑定其余的键

Gravity:输入复位的速度,仅用于类型为键/鼠标的按键

Dead:任何小于该值的输入值(不论正负值)都会被视为0,用于摇杆

Sensitivity:灵敏度,对于键盘输入,该值越大则响应时间越快,该值越小则越平滑。对于鼠标输入,设置该值会对鼠标的实际移动距离按比例缩放

Snap:如果启用该设置,当轴收到反向的输入信号时,轴的数值会立即置为0,否则会缓慢的应用反向信号值。仅用于键/鼠标输入

Invert:启用该参数可以让正向按钮发送负值,反向按钮发送正值 

Type

1.Key /Mouse:键/鼠标

2.Mouse Movement:鼠标移动和滚轮

3.Joystick Axis:摇杆

Axis:设备的输入轴(摇杆,鼠标,手柄等)

Joy Num:设置使用哪个摇杆,默认是接收所有摇杆的输入。仅用于输入轴和非按键

获取虚拟轴

 以下代码均返回的是布尔型,故只能判断虚拟按钮绑定的按键是否按下

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

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

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

 以下代码均放回的是浮点型,故可以判断虚拟轴的正负(-1~1),或说是按键在虚拟轴是代表的正负

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

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

镜头旋转

鼠标垂直移动使摄像机上下旋转,鼠标水平移动使摄像机左右旋转

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);
    }
}

玩家移动 

键盘垂直输入使飞机前后移动,键盘水平输入使飞机左右移动

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);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_53401568/article/details/128535085
今日推荐