Game Development Community

Mounting the camera to my player.

by Storm Kiernan · in Torque Game Builder · 06/13/2011 (8:13 pm) · 4 replies

I want the camera to follow my player. My assumption is that to do this I need to mount the sceneWindow2D to my player.

Here is my attempt at doing this:

function Person::onAdd(%this)
{
   if(!isObject($PersonGroup))
   {      
      $PersonGroup = new SimSet();
   }
   
   if(!isObject($player))
   {
      if(sceneWindow2D.getIsCameraMounted())
      {
         sceneWindow2D.dismount();
      }
      
      sceneWindow2D.mount(%this);
      
      $player = %this; 
   }
   else
   {
      %this.schedule(0, "aiThink");
   }
   
   $PersonGroup.add(%this);
}

The trouble is that the camera does not move with the player...

#1
06/14/2011 (10:30 pm)
So I am not sure whether or not this is a bug...but placing the mount() function inside of onAdd did not work. However after placing it in my updateMovement() it did work. Is there a specific time when objects cannot be mounted to? Any initialization wait times or something similar? Is there a function that fires off after all initialization has taken place?
#2
06/15/2011 (6:26 am)
The following behavior script is quite awesome if you haven't seen it. It allows you to easily set up camera bounds and stuff in the scene editor using the world bounds of a scene object. You can get the script and read the instructions here:
tdn.garagegames.com/wiki/TGB/Tutorials/Platformer/Camera
#3
06/15/2011 (6:28 am)
I don't believe there is. You could setup a scheduled call inside of onAdd, with a minor delay to help with that issue. One example might be
function Player::onInit(%this) {
    //attach your camera code here
}

function Player::onAdd(%this) {
    %this.schedule(500, onInit);
}

This should give the engine enough time to initialize the object to a point where it's possible to mount things appropriately.
#4
06/15/2011 (12:05 pm)
The fix to this was to create a scene object that was called GameStateManager and then use the onLevelLoaded for that object to manage the cameras mount.

smally, I did not end up testing your code since my code had worked prior to my seeing your post. although it is helpful to know that can be done!

Cory Leach, thank you for the link. I need to keep my nose in that section of tdn.