How can I get my camera to follow a clone after a certain trigger occurs?
The next issue I've been biting my teeth out on: After trigger occurs, I want my main Player camera to focus its "gaze" on the incoming enemy clone which has spawned. Problem is though, it wont. Is there something I'm missing in my code? How can I address the spawned "enemy(Clone)" properly? All done in Javascript btw.
Any help greatly appreciated!
var camLookAt : Transform;
var cameraUsed : Camera;
var Haunt : GameObject;
function SetCam()
{
if(PlayerSpawnTrigger.spawning == true)
{
camLookAt = GameObject.FindWithTag("Haunt").transform;
}
}
function Update()
{
if (GazeChecker.ForceGaze == true)
{
cameraUsed.transform.LookAt (camLookAt);
}
}
Answer by Ali-hatem · Mar 20, 2016 at 03:31 PM
if this script is attached to the player :
function SetCam()
{
if(PlayerSpawnTrigger.spawning == true)
{
// edetid
camLookAt.position = GameObject.FindWithTag("Haunt").transform.position;
}
}
function Update()
{
// you have to call function SetCam() here to detect enemy each time spawning unless you have other way.
function SetCam()
if (GazeChecker.ForceGaze == true)
{
cameraUsed.transform.LookAt (camLookAt);
}
}
but what if you have 2 enemies or more let me know
Hi, thanks for your reply!
I still keep getting an Exception error:
UnityException: Tag: Haunt is not defined. UnityEngine.GameObject.FindWithTag (System.String tag) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:297) ForceGaze.SetCam () (at Assets/Scripts/ForceGaze.js:9) ForceGaze.Update () (at Assets/Scripts/ForceGaze.js:18)
After your edits, my new code looks like this now:
var camLookAt : Transform;
var cameraUsed : Camera;
var Haunt : GameObject;
function SetCam()
{
if(PlayerSpawnTrigger.spawning == true)
{
camLookAt.position = GameObject.FindWithTag("Haunt").transform.position;
}
}
function Update()
{
SetCam();
if (GazeChecker.ForceGaze == true)
{
cameraUsed.transform.LookAt (camLookAt);
}
}
I did attach the "Haunt"-Prefab in the editor, but still i am getting this error...not sure where I am supposed to define it then...
And yes, i only need 1 enemy ;)
Answer by LethianGames · Mar 20, 2016 at 04:11 PM
Ok, so i found the problem:
When heading to Edit -> Project Settings -> Tags & Layers, i added a Tag called "Haunt". Then i went into my Haunt-Prefab and gave him the tag "Haunt".
Now the camera focuses on him (he is somewhere waaaaay off in the game world) but i want the camera to focus on the spawned Haunt(Clone) instead...any idea how to approach this?
Because I cant attach the tag to the Haunt(Clone)-Object alone, because it has not spawned yet...
sorry i don't get what you mean by Haunt(Clone) isn't it the same Haunt-Prefab
Yes, exactly - it is the same prefab. $$anonymous$$y Spawner script will instantiate a "Haunt(Clone)". And i need to put a tag on ONLY that clone. The code line looks like this:
for(var i = 0; i < How$$anonymous$$anyToSpawn; i++)
{
Instantiate (EnemyPrefab,position,Quaternion.identity);
}
is there a way to assign a created tag ONLY to this clone?
but you already said
so where is the problemi went into my Haunt-Prefab and gave him the tag "Haunt".
I added your code bit from this thread: http://answers.unity3d.com/questions/1158125/my-gameobject-remains-ingame-but-is-destroyed-in-t.html
And everything works fine now except of the problem described in my comment within the other thread. Thank you very much for your help :)
I'll close this thread now ;)
Your answer
Follow this Question
Related Questions
How get position from a game object? 5 Answers
How to Parent a Camera to the Head Bone with No Camera Shake? 0 Answers
How to make a GUI bar that counts items collected 0 Answers
How can I make the Player be destroyed when I touch the end of the camera? 1 Answer
How do you disallow an angle from going to a certain range while rotating? 0 Answers