Search code examples
unity-game-enginecamerapositiontransform

Unity change camera position with value from another script with functions


I am pretty new to Unity and i am trying to understand why this isn't working. What i am trying to do is move the camera with a Vector3.Lerp from the current camera position to the object position. This i want to do from functions in both scripts. Therefore i have two scripts one for the Camera and the other for the Cube.

This is the script for the camera

using UnityEngine;
using System.Collections;

public class CameraScript : MonoBehaviour {

    private Vector3 positionCamera;
    public Camera camera;

    void Start () {
        camera = GetComponent<Camera>();
        positionCamera = camera.transform.position;
    }

    void Update () {
        GoToTarget();
    }

    public void GoTo(Vector3 position)
    {
        positionCamera = Vector3.Lerp(positionCamera, position, Time.deltaTime);
    }

    public void GoToTarget()
    {
        Vector3 newpos = positionCamera;
        camera.transform.position = newpos;
    }
}

And the script for the Cube

using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour {


    public CameraScript cameraScript;
    private GameObject cube;
    private Vector3 cubePosition;
    private Vector3 newPosition;

    void Start () {
        cameraScript = new CameraScript();

        cube = this.gameObject;

        cubePosition = cube.transform.position;
        newPosition = transform.position;

    }

    // Update is called once per frame
    public void Update () {
        UpdatePosition();
    }

    public void UpdatePosition()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            cameraScript.GoTo(newPosition);
        }

    }
}

I pass the position from the cube script and send it to the GoTo function in the camera script. Then the camera should use the Vector3.Lerp to move the camera to the position of the cube. This however doesn't work.

How can i make this work with functions? Am i using the translate.position correctly for the camera? Or should i assign a game object first?

Thanks


Solution

  • I edited bit in CubeScript and I checked that it works fine for me while pressing spacebar. I wrote comments for what I changed... Go through it...

    using UnityEngine;
    using System.Collections;
    
    public class CubeScript : MonoBehaviour {   
    
        [SerializeField]
        private CameraScript cameraScript = null;// Changes here
    
        private GameObject cube;
        private Vector3 cubePosition;
        private Vector3 newPosition;
    
        void Start () {
            if (this.cameraScript == null) {
                this.cameraScript = this.gameObject.GetComponent<CameraScript>(); // Changes here
            }
    
            cube = this.gameObject;
    
            cubePosition = cube.transform.position;
            newPosition = transform.position;
        }
    
        // Update is called once per frame
        public void Update () {
            UpdatePosition();
        }
    
        public void UpdatePosition()
        {
            if (Input.GetKey(KeyCode.Space))
            {
                cameraScript.GoTo(newPosition);
            }
        }
    }
    

    There are different ways to call functions or variables, etc. from one to script to another, check here and my comment in this question for more information.