Game Development Community

Changing Camera Parameters via Script

by Jamie Uhrmacher · in Torque Game Builder · 01/24/2008 (12:29 pm) · 12 replies

I'm using the "Mount Camera To This" behavior to mount the camera to my player. Is there any way I can adjust the camera parameters while it's mounted to the player? I would like to have the camera zoom out when the player reaches certain score milestones. I'm a novice with Torque Script and would appreciate any advice on how to script this.

Thanks in advance!

#1
01/24/2008 (1:28 pm)
You should be able to use the setCurrentCameraZoom() function to do that. Basicly something like:

%obj.setCurentCameraZoom( 1 )

Where %obj is the scenewindow object that you have mounted to the player.
#2
01/24/2008 (2:53 pm)
Thank you - that works almost perfectly! The only problem is that the camera snaps to the new settings. Is there a way to interpolate to the new zoom value? For example, tell the camera to change to the new zoom value over a period of time?
#3
01/24/2008 (3:56 pm)
Go to Help -> Documentation inside of TGB, from here go to Reference -> TGB Reference on the left panel. Look for t2dSceneWindow and click methods. You should see a list of methods, specifically you will want to use

setTargetCameraZoom(zoomFactor)

and

startCameraMove([interpolationTime])

This should accomplish an interpolated camera zoom :)
#4
01/24/2008 (4:05 pm)
Can I call both of those commands sequentially in my script? Is the interpolation time designated as seconds?
(In the example below, would it take 5.5 seconds to zoom to 0.5?)

if(%dstObj.class $= "PlayerClass")
{
sceneWindow2D.setTargetCameraZoom(0.5);
startCameraMove(5.5);
}
#5
01/24/2008 (4:12 pm)
You are correct, it is seconds, this would be the proper way to do it (just tested it real quick too :).

sceneWindow2D.setTargetCameraZoom(0.5);
sceneWindow2D.startCameraMove(5.5);
#6
01/24/2008 (4:27 pm)
No, it would zoom in 0.5 seconds and move in 5.5.
#7
01/24/2008 (4:27 pm)
Thanks Matthew - I really appreciate the help. But I'm beginning to suspect my case if hopeless. I implemented your solution, but the camera does not change its zoom setting. If I change the command from setTarget to setCurrent, the camera instantly zooms. I've posted my whole script below - perhaps something unbeknownst to me is borking the whole thing. The goal of the script is to zoom the camera out when the player collides with a planet.

function Planets::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "PlayerClass")
{
sceneWindow2D.setTargetCameraZoom(0.5);
sceneWindow2D.startCameraMove(5.5);
%dstObj.modifyLife(%srcObj.lifeValue);
%srcObj.safeDelete();
}
}
#8
01/24/2008 (4:33 pm)
Hmm... just tested it again and it seems to be working as I had stated... What I exactly did and saw:

- Opened up BehaviorPlayground project
- Open up the console with ~
- typed in "SceneWindow2D.setTargetCameraZoom(0.5);"
- then typed in "SceneWindow2D.startCameraMove(30);"

What I see... It does the zoom across 30 seconds. Since the zoom isn't a linear interpolation (which is nice) at the large value of 30 seconds it does nothing (seemingly) for 10 seconds, then it starts to slowly zoom out and speed up to complete by the end of 30 seconds.
#9
01/24/2008 (4:36 pm)
I just copied and pasted

sceneWindow2D.setTargetCameraZoom(0.5);
sceneWindow2D.startCameraMove(5.5);

from your post and it worked for me... ensure you aren't setting the camera zoom elsewhere, possibly set the zoom to something other than 0.5, maybe 0.25.
#10
01/24/2008 (4:41 pm)
Thanks Matthew! By recreating your steps I noticed in the console that the script was failing because the camera was mounted to the player. I'll have to unmount it and trigger the command. The interpolation of the zoom is a little strange - it seems to pause for half the time designated. Many thanks for helping me get this working!
#11
01/24/2008 (4:45 pm)
Glad you got it working... you can change between two camera interpolation modes...

SceneWIndow2D.setCameraInterpolationMode(LINEAR);

or the default

SceneWIndow2D.setCameraInterpolationMode(SIGMOID);
#12
01/24/2008 (4:53 pm)
I'll try those interpolation modes out - thank you. I've modified my script to dismount the camera before zooming. Is it possible to dismount and remount the camera in the same function? I don't want you to write the entire script for me, but I just want to know if I'm trying to attempt the impossible. :-)

if(%dstObj.class $= "PlayerClass")
{
sceneWindow2D.dismount();
sceneWindow2D.setTargetCameraZoom(0.5);
sceneWindow2D.startCameraMove(4);
sceneWindow2D.mount($player, "0 0", 7, true);
%dstObj.modifyLife(%srcObj.lifeValue);
%srcObj.safeDelete();
}