第一人称视角(视角随鼠标移动)

挂到摄像机身上

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class FreeCamera : MonoBehaviour
{
    public float movementSpeed = 10f;
    public float fastMovementSpeed = 100f;
    public float freeLookSensitivity = 3f;
    public float zoomSensitivity = 10f;
    public float fastZoomSensitivity = 50f;
    private bool looking = false;

    void Update()
    {
        var fastMode = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
        var movementSpeed = fastMode ? this.fastMovementSpeed : this.movementSpeed;

        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position = transform.position + (-transform.right * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            transform.position = transform.position + (transform.right * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            transform.position = transform.position + (transform.forward * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            transform.position = transform.position + (-transform.forward * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.Q))
        {
            transform.position = transform.position + (transform.up * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.E))
        {
            transform.position = transform.position + (-transform.up * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.PageUp))
        {
            transform.position = transform.position + (Vector3.up * movementSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.PageDown))
        {
            transform.position = transform.position + (-Vector3.up * movementSpeed * Time.deltaTime);
        }

        if (looking)
        {
            float newRotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * freeLookSensitivity;
            float newRotationY = transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * freeLookSensitivity;
            transform.localEulerAngles = new Vector3(newRotationY, newRotationX, 0f);
        }

        float axis = Input.GetAxis("Mouse ScrollWheel");
        if (axis > 0)
        {
            GetComponent<Camera>().fieldOfView--;
        }
        else if (axis < 0)
        {
            GetComponent<Camera>().fieldOfView++;
        }

        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            StartLooking();
        }
        else if (Input.GetKeyUp(KeyCode.Mouse1))
        {
            StopLooking();
        }
    }

    void OnDisable()
    {
        StopLooking();
    }
    public void StartLooking()
    {
        looking = true;
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
    public void StopLooking()
    {
        looking = false;
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
    }
}

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

public class WaterReflection : MonoBehaviour
{
    public Transform probe;
    private Transform playerCamera;

    private void Start()
    {
        playerCamera = GetComponent<Transform>();
        probe.position = new Vector3(playerCamera.position.x, playerCamera.position.y, playerCamera.position.z);
    }
    void Update()
    {
        Vector3 pos = probe.position;
        pos.y = -Mathf.Abs(playerCamera.position.y);
        probe.position = new Vector3(playerCamera.position.x, pos.y, playerCamera.position.z);
    }
}

相关推荐

  1. 设置弹窗鼠标位置移动

    2024-07-16 14:00:03       50 阅读
  2. ARFoundation系列探索-第一人称视角技术编程

    2024-07-16 14:00:03       61 阅读
  3. Google人才选拔的独特视角

    2024-07-16 14:00:03       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 14:00:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 14:00:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 14:00:03       58 阅读
  4. Python语言-面向对象

    2024-07-16 14:00:03       69 阅读

热门阅读

  1. moment()获取时间

    2024-07-16 14:00:03       21 阅读
  2. 【Vue】 style中的scoped

    2024-07-16 14:00:03       18 阅读
  3. 乡镇集装箱生活污水处理设备处理效率高

    2024-07-16 14:00:03       16 阅读
  4. 2.CATIA:与其他程序及COM库集成的方式(1/2)

    2024-07-16 14:00:03       19 阅读
  5. 微服务中的 “负载均衡策略” 简介

    2024-07-16 14:00:03       26 阅读
  6. 深入了解 Oracle 版本命名中的 i、g 及 c

    2024-07-16 14:00:03       24 阅读
  7. oracle adg切换

    2024-07-16 14:00:03       28 阅读
  8. TCP、UDP、TCP与UDP的区别及联系

    2024-07-16 14:00:03       25 阅读