Unity版本:2019.4.31f1
通过鼠标控制视角,WASD控制前后左右移动,在游戏内实现摄像机的移动。
MoveCamera.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
public float speed = 3f;
public float rotateSpeed = 100f;
float h = 0f;
float v = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
Vector3 pos = transform.position;
pos += transform.forward * v * speed * Time.deltaTime;
pos += transform.right * h * speed * Time.deltaTime;
transform.position = pos;
float mx = Input.GetAxis("Mouse X");
float my = Input.GetAxis("Mouse Y");
transform.Rotate(
new Vector3( -my * rotateSpeed * Time.deltaTime, mx * rotateSpeed * Time.deltaTime, 0));
}
}