简体   繁体   中英

Transform.position of camera not going to correct position

Whenever I hard code a position for the camera to transform to it will work. However, If I try to give a position based on a particular game object I'm referencing, then the camera position appears to move to a completely random location.

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;
     }

its an Array. there are multiple scenes. Whenever I debug it shows the correct scene, however the position of x,y and z are different than the scene in question.

I have not enough information on your code but here is a working camera moving script. Just make an empty GameObject withing the scene and in that you make also empty GameObjects to define the desired positions. Reference the parent GameObject in this script and attach it to the camera.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM