可以直接放镜头或gameobject上,适合观看场景
Version 4:全改了
wasd/方向键:移动
shift:穿墙
Q/E:升降
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveAndMouseLook : MonoBehaviour
{
public Transform cam;
public bool allowShiftWall = true;
public bool allowSpaceFly = true;
public float mouseXSpeed;
public float mouseYSpeed;
public float moveSpeed;
private float speed;
public float xAngle;
public float yAngle;
float mouseX;
float mouseY;
//默认有Rigibody 和 CapsuleCollider.
private void Start()
{
speed = moveSpeed;
}
private void Update()
{
mouseX = Input.GetAxisRaw("Mouse X") * mouseXSpeed * Time.deltaTime;
mouseY = Input.GetAxisRaw("Mouse Y") * mouseYSpeed * Time.deltaTime;
xAngle += mouseX;
this.transform.eulerAngles = new Vector3(0, xAngle, 0);
yAngle += mouseY;
yAngle = Mathf.Clamp((yAngle % 360),130,230);
//cam.eulerAngles = new Vector3(yAngle, 180, 180);
cam.eulerAngles = new Vector3(yAngle, xAngle, 180);
Vector3 move = new Vector3(-Input.GetAxis("Horizontal") * speed / 100, 0, -Input.GetAxis("Vertical") * speed / 100);
if(move != Vector3.zero)
{
this.transform.position += Quaternion.Euler(0, xAngle, 0) * move;
}
if (allowSpaceFly && Input.GetKey(KeyCode.Q))
{
this.transform.position += Vector3.up;
}
else if (allowSpaceFly && Input.GetKey(KeyCode.E))
{
this.transform.position += Vector3.down;
}
if (allowShiftWall && Input.GetKeyDown(KeyCode.LeftShift))
{
speed = 10f;
this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
this.GetComponent<CapsuleCollider>().enabled = false;
}
if (allowShiftWall && Input.GetKeyUp(KeyCode.LeftShift))
{
speed = moveSpeed;
this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
this.GetComponent<CapsuleCollider>().enabled = true;
}
}
}
Version 3:基于Version2功能上简化了操作
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOVE : MonoBehaviour
{
public Transform leftRotateCenter;
public Transform rightRotateCenter;
private Transform currentRotateCenter;
public float speedLevel = 1;
private float mySpeed;
private void Update()
{
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
{
currentRotateCenter = null;
mySpeed = 0;
}
if (Input.GetMouseButtonDown(0))
{
currentRotateCenter = leftRotateCenter;
mySpeed = -100;
}
if (Input.GetMouseButtonDown(1))
{
currentRotateCenter = rightRotateCenter;
mySpeed = 100;
}
if (Input.GetKeyDown("-") && speedLevel > 0.5)
{
speedLevel -= 0.5f;
}
if (Input.GetKeyDown("=") && speedLevel < 3)
{
speedLevel += 0.5f;
}
}
private void LateUpdate()
{
if (currentRotateCenter != null)
{
this.transform.RotateAround(currentRotateCenter.position, Vector3.up, mySpeed * speedLevel * Time.deltaTime);
}
else if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
if (Input.GetKey("space"))
{
transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * speedLevel, 0);
}
else
{
transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * speedLevel);
}
}
}
}
Version 2:支持多种速度、分开了shift 和 space 的功能。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOVE: MonoBehaviour
{
public float horiSpeed = 10f;
public float vertSpeed = 3f;
public float speed = 2f;
public float maxSpeed = 4f;
public float minSpeed = 1f;
private void Update()
{
if (Input.GetKeyUp("=") && speed < maxSpeed)
{
speed += 1;
}
if (Input.GetKeyUp("-") && speed > minSpeed)
{
speed -= 1;
}
}
private void LateUpdate()
{
if (Input.GetKey("left shift"))
{
var turnArround = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed;
var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * (speed - 0.9f);
transform.Rotate(0, turnArround, 0);
transform.Translate(horizontal, 0, 0);
}
else if (Input.GetKey("space"))
{
var jump = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * 2 ;
transform.Translate(0, jump, 0);
}
else
{
var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * speed;
var vertical = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed;
transform.Translate(horizontal, 0, vertical);
}
}
}
Version 1:space 功能是升降,shift 功能是减速
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOVE : MonoBehaviour
{
public float fly = 10f;
public float Vert = 20.0f;
public float Hori = 100f;
void Update()
{
if (Input.GetKeyUp("left shift"))
{
Vert = 20f;
Hori = 100f;
fly = 10f;
}
if (Input.GetKeyDown("left shift"))
{
Vert = 4f;
Hori = 20f;
fly = 2f;
}
if (Input.GetKey("space"))
{
var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori;
var x = Input.GetAxis("Vertical") * Time.deltaTime * Vert;
transform.Translate(0, x, 0);
transform.Rotate(0, y, 0);
}
else
{
var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori;
var z = Input.GetAxis("Vertical") * Time.deltaTime * Vert;
transform.Translate(y, 0, z);
}
}
镜头围绕物体脚本