Ultimate Third Person Camera

TheLegend

New Member
Reaction score
10
[System] Advanced Third Person Camera

Advanced Third Person Camera
by TheLegend aka Codenamed​

Ever tried to make a third person camera that is almost perfect...well i did that
This camera system can:
-Rotate according to the users wish (includes normal unit rotation)
-Apply the height according to the terrain and unit flying height
-Avoid other objects
-Be switched from one unit to another
What you need:
NewGen World Editor
vJass
Ryoko's terrain pathing test map library (use this one)
Info:
JASS:

//Ultimate Third Person Camera System
//By TheLegend aka Codenamed
//First define some variables:
//      camera_main_unit              Unit Array - our main unit. This unit is the hero in the game. You can find this useful in RPG games
//      camera_target_unit            Unit Array - the camera objective. This is the unit wich the camera is locked on
//      camera_turn_degree            Real Array - the turn rate. Gives the player the ability to rotate the camera
//      camera_turnL               Boolean Array - Checks if the player wants to turn left
//      camera_turnR               Boolean Array - Checks if the player wants to turn right
//      tempunitgroup                   Unit Group - A unit group that will prevent leaks
//Copy the triggers to your map. Dont forget the library cause it wont work
//The library isnt mine but the camera system is so dont blaim me
//The Camera Swich Units trigger sets udg_camera_target_unit to the selected unit. If multiple units are selected it searches for the
//udg_camera_main_unit unit and sets it as the main camera unit
//To turn the camera press right and left
//Give me credits if you use the camera please

System:
JASS:

function Trig_Multiplayer_Camera_Actions takes nothing returns nothing

    local boolean b                     //The pathing boolean
    local integer i = 1                 //The player index counter
    local location l = Location(0,0)    //A point used to detect height
    local real v                        //The distance of the camera
    local real x                        //Coordinates x,y,z of two points
    local real y
    local real z
    local real x1
    local real y1
    local real z1

    loop
         if udg_camera_target_unit<i> != null and (GetWidgetLife(udg_camera_target_unit<i>) &gt; 0.405) then 
            set v = 25 //We set the minimum distance to 25
            set x = GetUnitX(udg_camera_target_unit<i>) //Get the x coordinate of the unit wich has the camera on it
            set y = GetUnitY(udg_camera_target_unit<i>) //Get the y coordinate of the unit wich has the camera on it
            call MoveLocation(l,x,y) //We move the l point to the units coordinates
            set z = GetLocationZ(l) // store the heignt as z
            set x1 = x + 64 * Cos((GetUnitFacing(udg_camera_target_unit<i>) + udg_camera_turn_degree<i>) * bj_DEGTORAD)
            set y1 = y + 64 * Sin((GetUnitFacing(udg_camera_target_unit<i>) + udg_camera_turn_degree<i>) * bj_DEGTORAD)
            call MoveLocation(l,x1,y1) 
            set z1 = GetLocationZ(l) //We find the point right in front of the unit&#039;s camera and get its height
            loop //Here begins the main loop for all players
                set x1 = x - v * Cos((GetUnitFacing(udg_camera_target_unit<i>) + udg_camera_turn_degree<i>) * bj_DEGTORAD)
                set y1 = y - v * Sin((GetUnitFacing(udg_camera_target_unit<i>) + udg_camera_turn_degree<i>) * bj_DEGTORAD)
                set b = IsTerrainWalkable(x1, y1) // This part uses the library I implemented to see is the point behind the unit in distance v is pathable
                if (b == false) then
                    set b = IsTerrainDeepWater(x1, y1) // We dont want to avoid water right...
                endif
                if (GetUnitFlyHeight(udg_camera_target_unit<i>) &gt; 0) then
                    set b = true
                endif // This is just to avoid distance changes if the unit is in the air
                if ((b == true) and (v &lt; 450)) then
                    set v = v + 25
                    elseif ((b == false) or (v == 450)) then
                    exitwhen true // This increases the camera distance until the system detects a non-pathable point.
                                  // Max distance can be changed, its the 450 integer
                endif
            endloop
            if GetLocalPlayer() == Player(i - 1) then //Make the system do the folowing stuff only for one player at the time
            call SetCameraField(CAMERA_FIELD_ROTATION, GetUnitFacing(udg_camera_target_unit<i>) + udg_camera_turn_degree<i>, 0.5) //Set the rotation of the cam to the units+our rotation wish
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, v, 0.2) //Set the distance to v
            call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, 335 + ((z1 - z) * 0.5), 0.32) //This is meant for the player to be able to see the &quot;top of the mountain&quot;
            call SetCameraField(CAMERA_FIELD_FIELD_OF_VIEW, 120, 0)
            call SetCameraField(CAMERA_FIELD_FARZ, 5000, 5) //How far the player will be able to see
            call SetCameraTargetController(udg_camera_target_unit<i>, 0, 0, false) //Locks the camera to the unit
            call SetCameraField(CAMERA_FIELD_ZOFFSET, GetUnitFlyHeight(udg_camera_target_unit<i>) + GetCameraField(CAMERA_FIELD_ZOFFSET) + z - GetCameraTargetPositionZ() + 128, -0.06) 
            call SetCameraField(CAMERA_FIELD_ZOFFSET, GetUnitFlyHeight(udg_camera_target_unit<i>) + GetCameraField(CAMERA_FIELD_ZOFFSET) + z - GetCameraTargetPositionZ() + 128, 0.06) //Height of the camera is adjusted here
          endif
        endif  
    exitwhen i == 1 // Change the value here for more players eg: i==9 for nine players
    set i = i + 1 //Next player is set
    call RemoveLocation(l) //We dont want leaks of points
    endloop //Loop ends for one player and starts for another
    set l = null //We remove the previous l
endfunction
function InitTrig_Multiplayer_Camera takes nothing returns nothing
    set gg_trg_Multiplayer_Camera = CreateTrigger(  )
    call TriggerRegisterTimerEvent(gg_trg_Multiplayer_Camera,0.06,true) //You can change the time if you want the cam to be faster
    call TriggerAddAction( gg_trg_Multiplayer_Camera, function Trig_Multiplayer_Camera_Actions )
endfunction
</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>

The initialization trigger:
Code:
Initialization
    Events
        Map initialization
    Conditions
    Actions
        -------- We add each user to the triggers listed below --------
        Player Group - Pick every player in (All players controlled by a User player) and do (Actions)
            Loop - Actions
                Trigger - Add to Camera Swich Units <gen> the event (Player - (Picked player) Selects a unit)
                Trigger - Add to TurnLEFT ON <gen> the event (Player - (Picked player) Presses the Left Arrow key)
                Trigger - Add to TurnRIGHT ON <gen> the event (Player - (Picked player) Presses the Right Arrow key)
                Trigger - Add to TurnLEFT OFF <gen> the event (Player - (Picked player) Releases the Left Arrow key)
                Trigger - Add to TurnRIGHT OFF <gen> the event (Player - (Picked player) Releases the Right Arrow key)
        -------- We set the main unit and the cam unit --------
        Set camera_main_unit[1] = Blood Mage 0004 <gen>
        Set camera_target_unit[1] = Blood Mage 0004 <gen>
The Unit Swicher:
Code:
Camera Swich Units
    Events
    Conditions
        (Owner of (Triggering unit)) Equal to (==) (Triggering player)
        ((Triggering unit) is A structure) Equal to (==) False
    Actions
        -------- we set the camera unit to no unit --------
        Set camera_target_unit[(Player number of (Triggering player))] = No unit
        -------- to prevent leaks we create a temp unit group --------
        Set tempunitgroup = (Units currently selected by (Triggering player))
        -------- we search for the main unit in selected units --------
        Unit Group - Pick every unit in tempunitgroup and do (Actions)
            Loop - Actions
                 Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        (Picked unit) Equal to (==) camera_main_unit[(Player number of (Owner of (Triggering unit)))]
                    Then - Actions
                        Set camera_target_unit[(Player number of (Triggering player))] = (Picked unit)
                    Else - Actions
                        Do nothing
                -------- we destroy the temp unit group --------
                Custom script:   call DestroyGroup (udg_tempunitgroup)
                -------- if there was no main unit in the selected units we set the camera to any selected unit --------
                 Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        camera_target_unit[(Player number of (Triggering player))] Equal to (==) No unit
                    Then - Actions
                        Set camera_target_unit[(Player number of (Triggering player))] = (Triggering unit)
                    Else - Actions
                        Do nothing
The turning system is in the map so just copy these. Give me some credit if you use the system.Here is the map:
View attachment UTPC.w3x
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
This hardly is the ultimate system. there are many reasons:
1). you should change those variable names, in most maps they are probably already taken, not to say they are confusing.
2). the triggers leak memory
3). many functions do not make any sense:
Code:
Set player_camera[(Player number of (Triggering player))] = No unit
Set player_camera[(Player number of (Triggering player))] = (Picked unit)
(the first line is completely useless
Code:
Do nothing
(does what it is called after)

besides these things which are rather easy to see you should also keep in mind, that the arrow keys are slow in multiplayer. movement systems using these are annoying instead of funny. real alternatives are rare though.


and i would never advice you to call anything "ultimative", thats the first negative thing one catchs when reading this thread.
 

TheLegend

New Member
Reaction score
10
I called it Ultimate cause i didnt come up with a better idea xD. Ok, about your 3rd line. I set the player cam to no unit to avoid the bug when you can see the units he loops (You actualy see it set the cam to each unit so it jumps arround a bit)
-I know the triggers leak (unit groups and stuff like that) but the jass code doesnt (the triggers were made half an hour ago when I was in a rush)
-wich variables, the globals or ordinary. The player variable is you in an RPG map so thats why its named (Ill rename it to main_camera_unit and current_camera_unit)
-the Do nothing is just a habit I have so its no big deal. Where do the triggers leak again?
EDIT

I fixed the leaks and the variable names. Hope you like it :)
 

TheLegend

New Member
Reaction score
10
I fixed all the leaks and changed the variable named. The camera target switcher works nice and the turning system is now debugged (the spin bug is now gone). Give me some credits if you use this system
 

Curo

Why am I still playing this game...?
Reaction score
109
"Ultimate" means final, last, and infers perfection.

OMG what a waste of my 1000th post...
 

TheLegend

New Member
Reaction score
10
try it and see for yourself. the system now has no leaks, its better than the old Advanced third person camera system. About the ultimate thing, what should i call it "Reloaded" - sounds like i stole it... "Advanced" - taken... "TheLegend's ..." - kind of boring. Stop criticizing its name and tell me what you like and dont like in the system (for gods sake...). Now that we cleared that (Ignore the name since I cant change it) tell me your critics on the system (jass code) cause the triggers are just ads...
 

tommerbob

Minecraft. :D
Reaction score
110
I didn't read the code, but my first impression is why on earth would you mix GUI with Jass in a system? Use one or the other, not both.

AceHeart already made an awesome Third Person Cam system. Why is this one better (or comparable)?
 

TheLegend

New Member
Reaction score
10
the jass code is the system. It gives you the possibility to rotate the camera as you wish, if a unit is in the air (flight) it wont avoid destructibles and it will set the height there where it actualy is (not the ground like the previous cam system). The previous camera system could not be moved from one unit to another (thats the GUI I added) and could not be manualy rotated. This one wont avoid water as a blocker. You can add a trigger to change the max and min distance and stuff like that... I wont complain
 

Alince

New Member
Reaction score
0
I was wondering if you can guide me how to make it a free camera movment around the hero with zoom option, trying to make an RPG map =3, I tried to edit but I cant seem to out run that zoom disable ^^""
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top