【发布时间】:2018-08-03 17:34:57
【问题描述】:
我正在使用 Unity 制作 3D 游戏,并将使用 Z、Q、S、 来移动相机D 键(我使用 Azerty 键盘) 和鼠标滚轮进行缩放。 escape 键将切换相机的移动。
我会让我的相机保持在最小和最大区域内。为此,我使用Vector3 类型的两个变量min 和max。这是 Main Camera 在 Unity 中的配置:
这是我的代码:
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
}
}
问题是当我使用按键移动相机时。我尝试了两行不同的代码,但两者都不能正常工作。这是我的问题。上面我的代码的注释行。
我不会同时使用两条线。
你能找到问题吗?
【问题讨论】:
-
您确定问题不在于您使用的是透视相机而不是正交相机吗?
-
@gkgkgkgk:我正在使用透视图。查看我的配置图片:i.stack.imgur.com/etgdy.png