Transform.position的相机不能正确的位置

时间:2018-04-24 23:45:09

标签: unity3d unityscript

每当我硬编码相机的位置进行转换就可以了。但是,如果我尝试根据我引用的特定游戏对象给出一个位置,那么相机位置似乎会移动到一个完全随机的位置。

void Update()
 {
     if(nextScene)            //Go to next scene (+1 scene).
     {
         sceneNumber++;
         transform.position = new Vector3(scenePos[sceneNumber].position.x, scenePos[sceneNumber].position.y, scenePos[sceneNumber].position.z);
         Debug.Log("pos is " + transform.position + "scene is" + sceneNumber);

         nextScene = false;
     }

它是一个数组。有多个场景。每当我调试时它会显示正确的场景,但是x,y和z的位置与所讨论的场景不同。

1 个答案:

答案 0 :(得分:1)

我的代码信息不足,但这是一个有效的相机移动脚本。只需使用场景制作一个空的GameObject,并在其中制作空的GameObjects以定义所需的位置。在此脚本中引用父GameObject并将其附加到相机。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public static class FloatDiscrepancy {
    public static float Accuracy = 0.00001f;

    public static float SqrAccuracy {
        get {
            return Accuracy * Accuracy;
        }
        private set { }
    }

    public static bool V3Equal(Vector3 a, Vector3 b){
        return Vector3.SqrMagnitude(a - b) < SqrAccuracy;
    }

    public static bool QuaternionEqual(Quaternion a, Quaternion b){
        return Quaternion.Angle (a, b) < Accuracy;
    }
}

public class CameraMovement : MonoBehaviour {
    public GameObject PositionObject;
    public float CamPosChangeSpeed = 5f;

    List<Transform> cameraPositions;
    bool inMotion = false;
    int currentPosition;

    // Use this for initialization
    void Start () {
        //get all camera positions automatically
        cameraPositions = new List<Transform> ();
        foreach (Transform child in PositionObject.transform) {
            cameraPositions.Add (child);
        }

        //set position index
        currentPosition = 0;

        //set camera start Popsition
        Camera.main.transform.position = cameraPositions [currentPosition].position;
        Camera.main.transform.rotation = cameraPositions [currentPosition].rotation;
    }

    // Update is called once per frame
    void Update () {
        if (inMotion) {
            moveCamera ();
        }
    }

    /// <summary>
    /// Switchs the camera position. Call it from other scripts or with an Event
    /// </summary>
    /// <param name="tP">Target Position as index for the list</param>
    public void SwitchCameraPosition(int targetPosition ) {
        if (cameraPositions.ElemenntAtOrDefault(targetPosition) != null){
            currentPosition = targetPosition;
            inMotion = true;
        }
    }

    /// <summary>
    /// Moves the camera.
    /// </summary>
    void moveCamera(){

        float disc = FloatDiscrepancy.Accuracy;
        //interpolate to new position
        Vector3 tmpPos = Vector3.Lerp (
            Camera.main.transform.position,
            cameraPositions [currentPosition].position,
            Time.deltaTime * CamPosChangeSpeed
        );
        //interpolate to new rotaion
        Quaternion tmpRot = Quaternion.Lerp (
            Camera.main.transform.rotation,
            cameraPositions [currentPosition].rotation,
            Time.deltaTime * CamPosChangeSpeed
        );
        Camera.main.transform.position = tmpPos;
        Camera.main.transform.rotation = tmpRot;

        FloatDiscrepancy.Accuracy = 0.01f;
        //if reached end position stop the motion and set endposition to be sure
        if (FloatDiscrepancy.V3Equal (tmpPos, cameraPositions [currentPosition].position) && FloatDiscrepancy.QuaternionEqual(tmpRot, cameraPositions[currentPosition].rotation)) {
            inMotion = false;
            Camera.main.transform.position = cameraPositions [currentPosition].position;
            Camera.main.transform.rotation = cameraPositions [currentPosition].rotation;
        }
        FloatDiscrepancy.Accuracy = disc;
    }
}