- Home /
I want the camera to follow just the X coordinate of the player.
using UnityEngine;
using System.Collections;
public class maincameraMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
public float pz = 0;
// Update is called once per frame
void Update () {
GameObject tofollow = GameObject.Find ("_player");
float px = tofollow.transform.position.x;
pz = px;
transform.position = new Vector3(px,0,0);
}
}
The camera ends up having the same X as the player but only when the game starts. It does not follow the player, even though this should work. This is for a 2D game, if you coul help that'd be great! Thanks :)
Comment
Best Answer
Answer by robertbu · Aug 02, 2014 at 09:22 PM
You don't want to call GameObject.Find() every frame. Try this:
using UnityEngine;
using System.Collections;
public class maincameraMovement : MonoBehaviour {
private GameObject tofollow;
void Start () {
toFollow = GameObject.Find ("_player");
}
void Update () {
Vector3 pos = transform.position;
pos.x = tofollow.transform.position.x;
transform.position = pos;
}
}
Your answer
Follow this Question
Related Questions
2D Game. Screen, Camera and coordinates. 0 Answers
2D Diablo style movement 1 Answer
Issue with clamping player to camera view 2 Answers
Adding A Dampening Effect On Camera Following Player? 1 Answer