1

このコードのほとんどを 1 年前のユニティ スレッドから取り出しました。実行するたびに、カメラが負の 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);
}
4

1 に答える 1