Question by
Tageos · Sep 09, 2015 at 12:26 AM ·
c#camera-movement
Make camera follow x-position of a gameobject
HellO!
I am trying to write a script for the main camera that makes the main camera follow a gameobjects x-postion. This is what i have:
using UnityEngine;
using System.Collections;
public class SmoothCamera2D1 : MonoBehaviour {
private Transform camFollow;
private GameObject Player;
public Vector3 camPos;
public Vector3 thisPos;
private float camPosX;
private float thisPosX;
void Start () {
camFollow = GameObject.FindGameObjectWithTag ("CamFollowPoint").transform;
Player = GameObject.FindGameObjectWithTag ("Player");
camPos = camFollow.position;
thisPos = this.gameObject.transform.position;
thisPosX = thisPos.x;
camPosX = camPos.x;
}
void Update () {
thisPosX = camPosX;
if (Player.transform.position.y > -1.5f)
{
}
}
}
Nothing happens though, the camera is still, while "camFollow" still moves. Anyone now what i am doing wrong? thisPosX = camPosX, seems pretty logical to me and i dont now why nothing happens. I appretiate all input on this!
Thank you!
//Taegos
Comment
Best Answer
Answer by Positive7 · Sep 09, 2015 at 12:36 AM
void Update () {
camPos = Camera.main.transform.position;
thisPos = this.gameObject.transform.position;
Camera.main.transform.position = new Vector3 (thisPos.x, camPos.y, camPos.z);
}