我从一个一年前的团结线程中解除了大部分代码。每当我运行它时,我的相机会很快地在负z方向上运行。我整天都在调整这些变量,但没有任何东西让它为我点击。
z每帧改变5个单位,这正是我为相机距离设置的。这似乎是理解这个问题的关键。我的部分问题是,我只能使用变换和欧拉角几乎无法掌握移动物体。谢谢你的时间。
public GameObject cameraTarget = null;
public float cameraSpeedX = 120.0f; //x sensitivity
public float cameraSpeedY = 120.0f; //y sensitivity
public float cameraVelocityX = 0.0f;
public float cameraVelocityY = 0.0f;
public float cameraRotationX = 0.0f;
public float cameraRotationY = 0.0f;
public float cameraLimitMinY = -20f;
public float cameraLimitMaxY = 80f;
public float cameraDistance = 5.0f;
public float cameraDdistanceMin = 0.5f;
public float cameraDistanceMax = 15.0f;
// Use this for initialization
void Start () {
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
Vector3 angles = transform.eulerAngles;
cameraRotationX = angles.x;
cameraRotationY = angles.y;
// Make the rigid body not change rotation
if(GetComponent<Rigidbody>()){GetComponent<Rigidbody>().freezeRotation = true;}
cameraTarget = gameObject;
}
void LateUpdate(){
if (cameraTarget){
if (Input.GetMouseButton(1)){
cameraVelocityX += cameraSpeedX * Input.GetAxis("Mouse X") * 0.02f;
cameraVelocityY += cameraSpeedY * Input.GetAxis("Mouse Y") * 0.02f;
}
cameraRotationY += cameraVelocityX;
cameraRotationX -= cameraVelocityY;
cameraRotationX = ClampAngle(cameraRotationX, cameraLimitMinY, cameraLimitMaxY);
//Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(cameraRotationX, cameraRotationY, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -cameraDistance);
Vector3 position = rotation * negDistance + cameraTarget.transform.position;
transform.rotation = rotation;
transform.position = position;
cameraVelocityX = Mathf.Lerp(cameraVelocityX, 0, Time.deltaTime * 2.0f);
cameraVelocityY = Mathf.Lerp(cameraVelocityY, 0, Time.deltaTime * 2.0f);
print ("POSITION:"+transform.position);
}
}
public static float ClampAngle(float angle, float min, float max){
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
答案 0 :(得分:0)
出于某种原因,我认为将相机脚本和游戏对象生成放在同一个对象中是个好主意。相机变换正在应用于自身,这导致相机无休止地试图自己落后。感谢mprivat的见解。