Unity基础——Input

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

写到这里吧,积少成多~

posted @   U3dever  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示