我正在使用Unity制作一个3D游戏,并将使用
Z轴
,
问
,
S公司
,
丁
钥匙
(我使用Azerty键盘)
和鼠标滚动来缩放。这个
逃跑
键将切换相机的移动。
我会让我的相机保持在最小和最大的区域内。为此,我使用两个变量
min
和
max
是那种
Vector3
. 这是统一的配置
Main Camera
:
这是我的代码:
using UnityEngine;
public class CameraController : MonoBehaviour
{
[Header("Speeds")]
public float panSpeed = 30;
public float scrollSpeed = 5;
[Header("Movement")]
public bool doMovement = true;
[Header("Min and max values")]
public Vector3 min;
public Vector3 max;
private void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
doMovement = !doMovement;
}
if (doMovement)
{
if (Input.GetKey(KeyCode.Z))
{
Move(Vector3.forward);
}
else if (Input.GetKey(KeyCode.S))
{
Move(Vector3.back);
}
else if (Input.GetKey(KeyCode.Q))
{
Move(Vector3.left);
}
else if (Input.GetKey(KeyCode.D))
{
Move(Vector3.right);
}
float scroll = Input.GetAxis("Mouse ScrollWheel") * 1000;
Vector3 pos = transform.position;
pos.y -= scroll * scrollSpeed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, min.y, max.y);
transform.position = pos;
}
}
private void Move(Vector3 direction)
{
Vector3 pos = direction * panSpeed * Time.deltaTime;
pos.x = Mathf.Clamp(pos.x, min.x, max.x);
pos.z = Mathf.Clamp(pos.z, min.z, max.z);
transform.Translate(pos, Space.World); // problem 1
transform.position = pos; // problem 2
}
}
问题是当我用钥匙移动相机时。我尝试了两行不同的代码,但它们都不适合aspected。这是我的问题。上面有我代码的注释行。
我不同时使用这两条线。
你能找到问题吗?