欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Input类虚拟轴案例

程序员文章站 2023-12-27 08:07:15
...

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

1. 案例1

1.1. 前台

Input类虚拟轴案例

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. 前台

Input类虚拟轴案例

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);
    }
}
相关标签: Unity

上一篇:

下一篇: