- Home /
How to make the camera acquire the position of a gameobject and start following it?
Hi!..I am new to unity....and this might be a really noob doubt....let me post the code first.... using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera_Controller : MonoBehaviour {
public GameObject player;
private Vector3 offset;
private Transform PlayerPosition;
// Use this for initialization
void Start () {
Vector3 startDist=new Vector3(46.0f,0.0f,-10.0f);
Debug.Log("11");
PlayerPosition=transform.Find("Pilot_Rigged");
Debug.Log("22");
Vector3 Playertemp=PlayerPosition.position;
Debug.Log("33");
transform.localPosition=startDist+Playertemp;
Debug.Log("44");
offset=transform.position-Playertemp;
}
// Update is called once per frame
void Update () {
transform.position=offset+player.transform.position;
}
}
here i am trying to acquire the position of the gameobject i want my camera to find and track.i used the transform.Find() to do that and assigned it to a transform variable....but when i am trying to access the position and assign it to the Vector3 "playertemp" variable it,s not working....AND the Debug.log are not printing the parts "33" and "44".....so i figured that the problem must be occuring in the Vector3 Playertemp=PlayerPosition.position; statement......what is wrong here??.....thanks in advance
Answer by kaosermail · Dec 10, 2018 at 10:46 AM
Try using: GameObject.Find("Pilot_Rigged").transform;
instead of transform.Find("Pilot_Rigged");
Your answer
Follow this Question
Related Questions
How can you move a camera a set amount of units based on the player's x position in a 2d game? 1 Answer
Why cant any operations be performed in camera when loaded from the previous scene? 0 Answers
Move camera when mouse is near the edges of the screen 1 Answer
Camera Goes white when moving with player, 1 Answer
How do i stop rigidbody2D bounce 1 Answer