Tuesday, October 9, 2012

Week 5 blog. Work with a camera.

Today i finished some of the coding things we had to do for the class ilab. It was for the most part pretty easy, but the sound part was the most time consuming of all of it. Though the final outcome of the sounds in game were pretty nice, so I guess it's not that bad. Another of the tasks I had to do was make a custom camera. I always liked isomorphic style games, so I wanted to make mine with that view type. The code online gave me a pretty nice one, though I had to adjust the pitch angle a bit.

I had to customize the code a bit, so I was thinking about what could be improved. While moving around I noticed the camera didn't stay behind you, which is often a problem in a game with shooting. This would have made it impossible to work with a gun, so to fix it I had to change the code to rotate the camera around the character. It was a pretty simple fix by using sin and cos to get the new x and y location for the camera based on the characters yaw rotation.

The code looked like this at the end:
//The "Rotation" is the player's.
out_CamLoc.X -= Cos(Rotation.Yaw * UnrRotToRad) * CamOffsetDistance;
out_CamLoc.Y -= Sin(Rotation.Yaw * UnrRotToRad) * CamOffsetDistance;

out_CamLoc.Z += Sin(IsoCamAngle * UnrRotToRad) * CamOffsetDistance;

//I also adjusted the camera yaw so that it would keep an eye on the player.

out_CamRot.Yaw = Rotation.Yaw;

/*This part of the code was just a little bit of something I found interesting. When turning with the mouse or a little with the left and right button, this would make the camera tilt in the direction you were turning.*/

out_CamRot.Roll = Rotation.Roll * 2;

There was also a small bug that I needed to fix. When I was playing with the camera I feigned and found that getting back up broke the character. I fixed this by making "var UTPlayerController UTPC;" declared at the top and writing in the code:

if (!bFeigningDeath) //bool if not feigning   {
     UTPC.SetBehindView(true);
//reset to behindview. feign resets to FPS view when in behindview.   }

After fixing up those few things I found the camera pretty well set up and ready to go. But then the obvious came to mind about what happens when a camera so far away is blocked.

After doing some searching I found that there is a trace function that will check if there's any collision with anything between the camera and player.

//If you search for trace function in unreal, you will find out what these parameters mean.
if ((Trace(HitLocation, HitNormal, out_CamLoc, Location, false, vect(0,0,0), )) != none)
   {
     out_CamLoc = HitLocation;
//this sets the camera to where the collision happened.
     out_CamLoc.Z = 100; //this was just to keep the camera at a head level height when against a wall.
   }