Aimbot-ParallelEnv/Assets/Script/Play/PlayerCamera.cs
Koha9 ff094aaba5 V3.0 追加控制模式,改进代码
追加控制模式,实现鼠标在地图中的坐标映射
追加控制UI
Todo:
游戏流程修改
游戏State对应修改
2023-04-09 23:35:38 +09:00

105 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCamera : MonoBehaviour
{
public float normalSpeed = 0.035f;
public float shiftSpeed = 0.06f;
public float zoomSpeed = -10.0f;
public float rotateSpeed = 0.1f;
public float maxHeight = 40f;
public float minHeight = 6f;
public Vector2 startMouseP;
public Vector2 dragMouseP;
private float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// shift to speed UP
if (Input.GetKey(KeyCode.LeftShift))
{
speed = shiftSpeed;
zoomSpeed = 20.0f;
}
else
{
speed = normalSpeed;
zoomSpeed = 10.0f;
}
float hsp = transform.position.y * speed * Input.GetAxis("Horizontal"); // horizontal movement speed
float vsp = transform.position.y * speed * Input.GetAxis("Vertical"); // vertical movement speed
float scrollSp = Mathf.Log(transform.position.y) * -zoomSpeed * Input.GetAxis("Mouse ScrollWheel"); // scroll speed
// camera height limit
if (transform.position.y + scrollSp > maxHeight)
{
scrollSp = maxHeight - transform.position.y;
}
else if (transform.position.y + scrollSp < minHeight)
{
scrollSp = minHeight - transform.position.y;
}
/* if((transform.position.y <= maxHeight) && (scrollSp > 0))
{
scrollSp = 0;
}else if((transform.position.y >= minHeight)&& (scrollSp < 0))
{
scrollSp = 0;
}
if((transform.position.y + scrollSp) > maxHeight)
{
scrollSp = maxHeight - transform.position.y;
}else if((transform.position.y + scrollSp) < minHeight)
{
scrollSp = minHeight - transform.position.y;
}*/
Vector3 verticalMove = new Vector3(0,scrollSp,0); // vertical movement
Vector3 lateralMove = hsp * transform.right; // lateral movement in global world ignore camera facing
Vector3 fowardMove = transform.forward; // forward movement in global world ignore camera facing
fowardMove.y = 0; // ignore y axis
fowardMove = vsp * fowardMove.normalized; // normalize the vector
Vector3 move = verticalMove + lateralMove + fowardMove; // total movement
transform.position += move; // move the camera
cameraRotation();
}
void cameraRotation()
{
// camera rotation while press middle mousebutton
if(Input.GetMouseButtonDown(2))
{
startMouseP = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
dragMouseP = Input.mousePosition;
float dx = (dragMouseP - startMouseP).x * rotateSpeed;
float dy = (dragMouseP - startMouseP).y * rotateSpeed;
transform.rotation *= Quaternion.Euler(new Vector3(0, dx, 0));
transform.GetChild(0).rotation *= Quaternion.Euler(new Vector3(-dy, 0, 0));
startMouseP = dragMouseP;
}
}
}