Catalogue

Monday, August 8, 2016

Scripting 5: Script: Camera_Control

Camera_Control speaks for itself. It's the camera script.


This is an overview of the Camera_Control Script.
VOID Start()  Only used to set certain variables that can't be set earlier.
VOID Update()  Does everything.
  • First InputCheck is called and player data is read.
    • playerspeed = player's.hspeed*3+player's.endlessrun.
      • hspeed records horizontal speed.
      • endlessrun is automatic movement, used in some bossfights.
    • playerfall = player's.rigidbody2D.velocity.y;
  • holding right adds +1 to the variable "aboutFaceLag." If it's value becomes 100, the camera will now stand to the right side of the player. The opposite is done when holding left.
    • This allows you to see ahead of the character, and adds a delay to camera movement when the player is changing directions.
  • If playerfall<15(player is not jumping), the camera stands in the default location.
  • If playerfall>15(player is jumping), the camera tries to stay still.
  • If playerfall<-10(player is falling), the camera attempts to keep you in sight.
  • If playerfall<-24(player is in a long fall), the camera tries to get below you.
  • Wall climbing
    • Reads the player's fspeed, and adjusts to look over or under the player depending which way they're going.
  • If you tap left or right, threatTest is called. If an enemy is found within the aimed vicinity, the camera moves slightly to get them into view.
  • goingLow is one of many variables used to control the y offset of the camera from the player. If you hold down, goingLow decreases to move the camera lower.
  • At this point, custom movements are taken into account. If the camera is moving automatically, most of the above is ignored, and the camera instead moves based on a value.
    • The player is not allowed to exit the camera's field of view while it's moving automatically.
    • If the camera is moving horizontally, it will attempt to keep a strict y position.
  • Rotations are set through a Quaternion.Lerp by 0.01f;
  • The field of view and z offset are both set through Mathf.Lerp by 0.02f;
  • Earthquaking moves the camera around rapidly for a set amount of time. 
  • The camera tests for walls and water.
    • If there's a wall overhead, the camera will avoid rising.
    • If there's water underneath, the camera will try to stay above it.
  • Colorizer: Reads the main light's script and changes the background color accordingly.
At this point the final camera movements are being calculated.
  • First the camera's movement is smoothened. 
    • playerposSmoother = Vector3.Lerp(location, target, 0.98f);
  • startx and starty = playerposSmoother;
    • startz = playerposSmoother + zoffset(the default distance the camera should keep);
  • The final variable "Target" is now calculated.
    • target.x = startx + playerspeed/3(player's running speed) + xoffset(default distance to keep);
    • If y can move freely then target.y = starty + yoffset;
    • If the camera is too close to the bottom of the level, target.y won't fall below 10;
    • If the z position of the camera can move freely, target.z = startz;
Between the first and last calculations, the overrides are set.
  • The camera uses a raycast to stay away from walls in order to prevent clipping or xraying.
  • A circlecast checks for focusers. 
    • If any is found, focusCheck = true; and FindFocus() is called.
    • If none is found then focusCheck = false, and the rotation and field of view are reset;
And now the final movements are set.
  • moving.x = Mathf.Lerp(current x position,target.x,cameraspd*50*Time.deltatime);
  • moving.y = Mathf.Lerp(current y position, target.y, cameraspd*mathf.Abs(currentpos.y-target.y)(changes speed based on distance) * 2.5f * Time.deltaTime);
  • moving.z changes similarly to movingx, just slightly slower (30 instead of 50);
  • transform.position = moving;
  • If the distance is too great or the player dies, the camera snaps to the target via SetCamFast();
 IEnumerator Camflix(float shiftSpeed)  Turns the camera on and off, used after the player dies and during the level's start while things are still loading.
VOID SetCamFast() Quickly resets the camera. 
VOID setFields(int settingT) Changes the field of view between presets. 
VOID ThreatTest() Checks for surrounding enemies. If there is an enemy within a certain location, the camera will move towards it when changing directions. 
VOID FindFocus()  Focuses tell the camera where to go while the player is within its field. Focusers can affect field of view, rotation, and location. 
VOID InputCheck(int whatCheck = 0)  This is a basic input checking function that reads the arrow keys, WASD, joypad, and joystick all at once.

No comments:

Post a Comment