So as i was working on my camera code i came across a rather big issue. While i finally figured out how to move the camera to the players Y position smoothly, it happened to also place the player’s Z position to the camera’s Z position. This is completely unintentional and i can’t for the life of me figure out why this is happening as i can’t see where i instruct the player to move to the camera. Long story short: anybody got any idea how to fix this? (little tip: don’t stop coding for a month…)
CODE:
using UnityEngine;
using System.Collections;
public class cameraMovement : MonoBehaviour {
// Use this for initialization
public Transform playerTransform;
public Transform cameraTransform;
public Transform moveToTransform;
public Vector3 tempCalcVectorX;
public Vector3 tempCalcVectorY;
public float newCameraYPos;
public float cameraYPos;
public float tempCameraYPos;
public float curHeightOffset;
public float maxHeightOffset;
public float moveToSpeed;
void Awake () {
moveToTransform = GameObject.Find("player").transform;
cameraTransform = transform;
playerTransform = GameObject.Find("player").transform;
}
// Update is called once per frame
void Update () {
newCameraYPos = playerTransform.transform.position.y;
cameraYPos = transform.position.y;
curHeightOffset = newCameraYPos - cameraYPos;
if (curHeightOffset <= maxHeightOffset) {
moveCameraToPlayer();
}
tempCalcVectorX = new Vector3(playerTransform.transform.position.x, 0, 0);
tempCalcVectorY = new Vector3(0, playerTransform.position.y, -10);
moveToTransform.position = tempCalcVectorY;
}
void moveCameraToPlayer() {
float step = moveToSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(moveToTransform.position, transform.position, step);
}
}