Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

UDK action game - 3rd person camera code

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • UDK action game - 3rd person camera code

    Finally got the basic 3rd person action game camera code done. Hopefully some will find this useful. This is a basic orbit camera with zoom on the mouse wheel. I also added a basic trace to keep the cam out of level geometry. It is not as fully featured as I would like, but this is just the basics. The real trick to this is keeping the camera behind the playercontroller but only updating pawn rotation when moving. You can see that in action here:

    http://www.youtube.com/watch?v=cREzOaBF7FQ

    and here's the pawn code:
    Code:
    class ActionPawn extends UTPawn
    config(ActionGame);
    
    var float CamOffsetDistance; //distance to offset the camera from the player in unreal units
    var float CamMinDistance, CamMaxDistance;
    var float CamZoomTick; //how far to zoom in/out per command
    var float CamHeight; //how high cam is relative to pawn pelvis
    
    //stop aim node from aiming up or down
    simulated event PostInitAnimTree(SkeletalMeshComponent SkelComp)
    {
    	super.PostInitAnimTree(SkelComp);
    	AimNode.bForceAimDir = true; //forces centercenter
    }
    
    simulated event PostBeginPlay()
    {
    	super.PostBeginPlay();
    	`Log("Custom Pawn up"); //debug
    
    }
    
    //override to make player mesh visible by default
    simulated event BecomeViewTarget( PlayerController PC )
    {
       local UTPlayerController UTPC;
    
       Super.BecomeViewTarget(PC);
    
       if (LocalPlayer(PC.Player) != None)
       {
          UTPC = UTPlayerController(PC);
          if (UTPC != None)
          {
             //set player controller to behind view and make mesh visible
             UTPC.SetBehindView(true);
             SetMeshVisibility(UTPC.bBehindView); 
             UTPC.bNoCrosshair = true;
          }
       }
    }
    
    //only update pawn rotation while moving
    simulated function FaceRotation(rotator NewRotation, float DeltaTime)
    {
    	// Do not update Pawn's rotation if no accel
    	if (Normal(Acceleration)!=vect(0,0,0))
    	{
    		if ( Physics == PHYS_Ladder )
    		{
    			NewRotation = OnLadder.Walldir;
    		}
    		else if ( (Physics == PHYS_Walking) || (Physics == PHYS_Falling) )
    		{
    			NewRotation = rotator((Location + Normal(Acceleration))-Location);
    			NewRotation.Pitch = 0;
    		}
    		
    		SetRotation(NewRotation);
    	}
    	
    }
    
    
    //orbit cam, follows player controller rotation
    simulated function bool CalcCamera( float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV )
    {
    	local vector HitLoc,HitNorm, End, Start, vecCamHeight;
    
    	vecCamHeight = vect(0,0,0);
    	vecCamHeight.Z = CamHeight;
    	Start = Location;
    	End = (Location+vecCamHeight)-(Vector(Controller.Rotation) * CamOffsetDistance);  //cam follow behind player controller
    	out_CamLoc = End;
    
    	//trace to check if cam running into wall/floor
    	if(Trace(HitLoc,HitNorm,End,Start,false,vect(12,12,12))!=none)
    	{
    		out_CamLoc = HitLoc + vecCamHeight;
    	}
    	
    	//camera will look slightly above player
       out_CamRot=rotator((Location + vecCamHeight) - out_CamLoc);
       return true;
    }
    
    exec function CamZoomIn()
    {
    	`Log("Zoom in");
    	if(CamOffsetDistance > CamMinDistance)
    		CamOffsetDistance-=CamZoomTick;
    }
    
    exec function CamZoomOut()
    {
    	`Log("Zoom out");
    	if(CamOffsetDistance < CamMaxDistance)
    		CamOffsetDistance+=CamZoomTick;
    }
    
    defaultproperties
    {
    	CamHeight = 40.0
    	CamMinDistance = 40.0
    	CamMaxDistance = 350.0
       	CamOffsetDistance=250.0
    	CamZoomTick=20.0	
    }
    and my playercontroller:
    Code:
    class ActionPlayerController extends UTPlayerController
    	config(ActionGame);
    
    //Update player rotation when walking
    state PlayerWalking
    {
    ignores SeePlayer, HearNoise, Bump;
    
    
       function ProcessMove(float DeltaTime, vector NewAccel, eDoubleClickDir DoubleClickMove, rotator DeltaRot)
       {
    	  local Vector tempAccel;
    		local Rotator CameraRotationYawOnly;
    		
    
          if( Pawn == None )
          {
             return;
          }
    
          if (Role == ROLE_Authority)
          {
             // Update ViewPitch for remote clients
             Pawn.SetRemoteViewPitch( Rotation.Pitch );
          }
    
          tempAccel.Y =  PlayerInput.aStrafe * DeltaTime * 100 * PlayerInput.MoveForwardSpeed;
          tempAccel.X = PlayerInput.aForward * DeltaTime * 100 * PlayerInput.MoveForwardSpeed;
          tempAccel.Z = 0; //no vertical movement for now, may be needed by ladders later
          
    	 //get the controller yaw to transform our movement-accelerations by
    	CameraRotationYawOnly.Yaw = Rotation.Yaw; 
    	tempAccel = tempAccel>>CameraRotationYawOnly; //transform the input by the camera World orientation so that it's in World frame
    	Pawn.Acceleration = tempAccel;
       
    	Pawn.FaceRotation(Rotation,DeltaTime); //notify pawn of rotation
    
        CheckJumpOrDuck();
       }
    }
    
    //Controller rotates with turning input
    function UpdateRotation( float DeltaTime )
    {
    local Rotator   DeltaRot, newRotation, ViewRotation;
    
       ViewRotation = Rotation;
       if (Pawn!=none)
       {
          Pawn.SetDesiredRotation(ViewRotation);
       }
    
       // Calculate Delta to be applied on ViewRotation
       DeltaRot.Yaw   = PlayerInput.aTurn;
       DeltaRot.Pitch   = PlayerInput.aLookUp;
    
       ProcessViewRotation( DeltaTime, ViewRotation, DeltaRot );
       SetRotation(ViewRotation);
    
       NewRotation = ViewRotation;
       NewRotation.Roll = Rotation.Roll;
    
       if ( Pawn != None )
          Pawn.FaceRotation(NewRotation, deltatime); //notify pawn of rotation
    }   
    
    DefaultProperties
    {
        
    }
    to get the zoom exec's to work you must edit your DefaultInput.ini under Game/Keyboard bindings:
    Code:
    ;.Bindings=(Name="MouseScrollUp",Command="GBA_PrevWeapon")
    ;.Bindings=(Name="MouseScrollDown",Command="GBA_NextWeapon")
    
    .Bindings=(Name="MouseScrollUp",Command="CamZoomIn")
    .Bindings=(Name="MouseScrollDown",Command="CamZoomOut")
    this removes the weapon swap on mouse scroll and adds in my custom zoom

    if anyone has questions ask away! =D

  • #2
    that's a nice one. well best 3rd-P cam i found here

    just 1 little problem
    I extended my game info from UTGame and I can't see the players mesh. just a gun floating in the air. what should I do to fix it?

    Comment


    • #3
      Check your Pawn defualtproperties, I don't think UDK likes the mesh being changed there anymore. I still have yet to find a proper way to change it without getting the same thing you have. Likely is something with the familyinfo stuff in the documentation here:
      http://udn.epicgames.com/Three/UDKCustomCharacters.html

      Comment


      • #4
        Try adding the following code from the Camera Technical Guide to your Pawn file. It should make the player mesh visible.

        Code:
        //override to make player mesh visible by default
        simulated event BecomeViewTarget( PlayerController PC )
        {
           local UTPlayerController UTPC;
        
           Super.BecomeViewTarget(PC);
        
           if (LocalPlayer(PC.Player) != None)
           {
              UTPC = UTPlayerController(PC);
              if (UTPC != None)
              {
                 //set player controller to behind view and make mesh visible
                 UTPC.SetBehindView(true);
                 SetMeshVisibility(UTPC.bBehindView);
                 UTPC.bNoCrosshair = true;
        
              }
           }
        }

        Comment


        • #5
          Ah yes, that will be needed. Sorry I meant to copy that part of the code as well.

          Comment


          • #6
            Wow that is a really nice 3rd person cam, I was thinking of one just like that for in my game, however... I want it to be a FPS with the option of changing to 3rd person when you press 'm' I know it's all in the default input but i want it so that if it were in first person and m is pressed it goes to the third person cam and overwrites the scroll weapon thing. But then if it is in third person, and m is pressed it goes back to first person and the scroll weapons comes back. How would I able to achieve this?

            And how would i make it so the camera is fixed to the back of the pawn, so everytime the pawn moves the camera makes sure it is locked on the pawns back, so when it turns the camera turns with the pawn. And would it be possible to update the camera to revert back to it's 'default' position if
            i were to orbit around the pawn and then when he starts running in a direction the camera goes back to being locked on it's back.

            And also Iv notice the crosshair in 3rd person is inaccurate? is this because the cross hair drawn from the HUD class rather then the camera class?

            Thanks,
            aZzy

            Comment


            • #7
              Sorry for the double post.

              I'm using my iPod touch and realized I accidentally wrote inhere instead of I were. Tried to edit it but there is no scroll bar to access the rest of what was written.

              Sorry fixed it -.-

              My apologies

              Comment


              • #8
                Great work!can I use it for non commercial?
                <.<
                >.>

                Comment


                • #9
                  is there any way to keep gun aiming same direction when using UTPC.SetBehindView(true);

                  or another solution for character's mesh?

                  Comment


                  • #10
                    great work

                    Comment


                    • #11
                      Did you have to change anything in your config files besides default input to run this? Im having issues getting the editor to recognize this camera and run it. Can you elaborate on how you got this working a little more? Great work.

                      Comment


                      • #12
                        Originally posted by apocalypse904 View Post
                        Did you have to change anything in your config files besides default input to run this? Im having issues getting the editor to recognize this camera and run it. Can you elaborate on how you got this working a little more? Great work.
                        Check your log, make sure you see my "Custom pawn up" debug line, else your gameinfo class is not loading my pawn code proper. If you are using PIE you must change the default PIE gametype to your gameinfo. Search in documentation for details.

                        Also, thanks to all for the compliments, and yes you can use this code for whatever. There's hardly any good 3rd person camera code, so I made this for all to use.

                        Comment


                        • #13
                          is a good camera for the utpawn, i have tested it, but i 'm having one issue, when the player get the weapon he can shot, but the weapon doesn't show, i think that the weapon position must be update in the camera code or something like that, but i don't know how, some ideas ?

                          Comment


                          • #14
                            I made it work but I have two problems:
                            1.- The mouse scroll zoom in/out doesn't work
                            2.- Theres some kind of weapon object flying back of the character.

                            Comment


                            • #15
                              Originally posted by Argaroth View Post
                              I made it work but I have two problems:
                              1.- The mouse scroll zoom in/out doesn't work
                              2.- Theres some kind of weapon object flying back of the character.
                              1- make sure you changed the bindings in your DefualtInput.ini, all else fails you can call the function from the console, command is CamZoomIn and CamZoomOut

                              2- this code does not affect weapons, likely some other bug somewhere, check your sockets and attachment class

                              Comment

                              Working...
                              X