Unity基礎——Input

U3dever發表於2024-05-24

Unity基礎——Input


1.滑鼠輸入

//我們在這段程式碼最後實現一個基本用滑鼠控制視角的功能
using UnityEngine;

class Script1 : MonoBehaviour 
{
    [SerializeField]
    private float mouseSensitivity = 30f;
    
    private float xRotation = 0f; //當前的旋轉角度
    
    void Update()
    {
        if (Input.GetButtonDown(0)) //檢測滑鼠左鍵是否按下, 1和2表示右鍵和中鍵
        {
            //Do something.
            Fire();
        }
        
        if (Input.GetButtonUp(0)) //檢測滑鼠左鍵是否抬起
        {
            //Do something.
            Ease();
        }
        
        if (Input.GetButton(0)) //檢測滑鼠左鍵是否長按
        {
            //Do something.
            Shoot();
        }
        
        //如果是GetAxis()而不是GetAxisRaw(),那麼將會返回一個介於[0, 1]的浮點數,後者僅會返回1和0
        //在需要實現一些效果時靈活可以使用,比如想要寫一個玩家控制器,且鬆開鍵盤後人物還要滑動一段距離而不是立刻停下,
        //就可以使用GetAxis()
        float yRotation = mouseSensitivity * Time.deltaTime * GetAxis("Mouse X"); //檢測x軸向的滑鼠移動
        float xxRotation = mouseSensitivity * Time.deltaTime * GetAxis("Mouse Y"); //檢測y軸向的滑鼠移動
        
        xRotation -= xxRotation;
        
        Math.Clamp(xRotation, -90f, 90f);
        
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        
        transform.Rotate(Vector3.up, yRotation, Space.World);
        //或者transform.Rotate(Vector3.up * yRotation, Space.World);
	}
}

2.鍵盤輸入

using UnityEngine;

class Script2 : MonoBehaviour
{
	void Update()
	{
		if (Input.GetKeyDown(KeyCode.W)) //檢測某個按鍵是否被按下
        {
            Move(Direction.Forward);
        }
        
        if (Input.GetKeyUp(KeyCode.S)) //檢測某個按鍵是否抬起
        {
            Move(Direction.Back);
        }
        
        if (Input.GetKey(KeyCode.Enter)) //檢測某個鍵是否被長按
        {
            Confirm();
        }
        
        float u = GetAxis("Horizontal"); //預設設定是AD鍵
        float v = GetAxis("Vertical"); //預設是WS鍵
	}
}

3.練習

//1.建立一個坦克預製體,用鍵盤控制其左右移動,用滑鼠的上下拖動控制其炮管的上下旋轉,用滑鼠的左右拖動模擬炮臺的旋轉;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.Cursor;

public class InputPractice : MonoBehaviour
{
    [SerializeField] private Transform root;
    [SerializeField] private Transform body;

    public float mouseSensitivity = 100f;
    public float moveSpeed = 44f;

    private float _rootRotation;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked; //鎖定游標
        Cursor.visible = false; //隱藏游標
    }

    // Update is called once per frame
    private void Update()
    {
        GunBarrel();
        Move();
    }

    private void GunBarrel()
    {
        var u = Input.GetAxisRaw("Mouse X") * mouseSensitivity * Time.deltaTime;
        var v = Input.GetAxisRaw("Mouse Y") * mouseSensitivity * Time.deltaTime;

        _rootRotation -= v;
        _rootRotation = Mathf.Clamp(_rootRotation, -44f, 44f);
        root.transform.localRotation = Quaternion.Euler(_rootRotation, 0f, 0f);
        
        body.Rotate(Vector3.up, u, Space.World);
    }

    private void Move()
    {
        var hor = Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime;
        var ver = Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime;
        
        transform.Translate(new Vector3(ver, 0, hor), Space.Self);
    }
}


//2.創造一個蠟燭光源(點光源),實現閃爍的效果
using System;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using UnityEngine;

public class LightPractice : MonoBehaviour
{
    public float maxIntensity;
    public float minIntensity;
    public float nowIntensity;
    public float targetIntensity;

    private Light _light;
    
    [SerializeField] private float flashingRate;
    
    private void Start()
    {
        _light = GetComponent<Light>();
        nowIntensity = minIntensity;
    }

    private void Update()
    {
        if (Mathf.Abs(nowIntensity - minIntensity) < 0.01f) targetIntensity = maxIntensity;
        if (Mathf.Abs(nowIntensity - maxIntensity) < 0.01f) targetIntensity = minIntensity;

        nowIntensity = Mathf.Lerp(nowIntensity, targetIntensity, flashingRate * Time.deltaTime);
        
        _light.intensity = nowIntensity;
    }
}

//3.用直射光模擬日夜變化
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;

public class LightDayAndNight : MonoBehaviour
{
    public float rotateSpeed;
    // public float dayTime; //可以自定義一天的時間, 以此封裝一個更加複雜的天氣系統
    
    // Update is called once per frame
    private void Update()
    {
        transform.Rotate(Vector3.left, rotateSpeed * Time.deltaTime, Space.World);
    }
}

寫到這裡吧,積少成多~

相關文章