Input类虚拟轴案例

Unity 学习笔记汇总
官方API使用文档

1. 案例1

1.1. 前台

在这里插入图片描述

1.2. 代码

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

public class test1 : MonoBehaviour
{

    public GameObject cube;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float speed = 1;
        float x = Input.GetAxis("Horizontal");
        Debug.Log(x); //A键范围:(-1, 0),D:(0,1)

        float y = Input.GetAxis("Vertical");
        Debug.Log(y); //S键范围:(-1, 0),W:(0,1)

        if (Input.GetAxis("Horizontal") > 0)
        {
            Debug.Log("D");
        }
        else if (Input.GetAxis("Horizontal") < 0)
        {
            Debug.Log("A");
        }

        if (Input.GetAxis("Vertical") > 0)
        {
            Debug.Log("W");
        }
        else if (Input.GetAxis("Vertical") < 0)
        {
            Debug.Log("S");
        }

        cube.transform.Translate(new Vector3(x, y, 0) * speed, Space.Self);
    }
}

2. 案例2

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

public class test2 : MonoBehaviour
{
    public float cameraField;
    // Start is called before the first frame update
    void Start()
    {
        cameraField = Camera.main.transform.GetComponent<Camera>().fieldOfView;
        cameraField = Camera.main.fieldOfView;
        cameraField = GameObject.Find("Main Camera").transform.GetComponent<Camera>().fieldOfView;
        cameraField = GameObject.FindWithTag("MainCamera").transform.GetComponent<Camera>().fieldOfView;
    }

    // Update is called once per frame
    void Update()
    {
        //按键
        if (Input.GetKeyDown(KeyCode.W))
        {
            cameraField += 2;
        }

        //鼠标滚轮
        float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
        //鼠标滚轮向上滚动是正值,向下滚动是负值
        Debug.Log(mouseScrollWheel);
        if (mouseScrollWheel>0)
        {
            cameraField += 2;
            if (cameraField >120)
            {
                cameraField = 120;
            }
        }
        else if(mouseScrollWheel<0)
        {
            cameraField -= 2;
            if (cameraField < 60)
            {
                cameraField = 60;
            }
        }
        Camera.main.fieldOfView = cameraField;
    }
}

3. 案例3

3.1. 前台

在这里插入图片描述

3.2. 代码

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

public class test : MonoBehaviour
{
    public GameObject plane, cube;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float x, y;

        x = Input.GetAxis("Mouse X");
        y = Input.GetAxis("Mouse Y");
        Debug.Log("" + x + ", " + y + ", " + Input.GetAxis("Mouse X") + ", " + Input.GetAxis("Mouse Y"));

        //cube.transform.Rotate(new Vector3(x, y, 0), Space.World);
        plane.transform.Rotate(new Vector3(0, -y, 0), Space.Self);
    }
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读