Unity Development:
Unity 3.5 game mechanics testing. Created an obstacle coarse to test player controller and mechanics script's and 3rd person camera script.
Scripts coded in C#. Scripts included: player input and animation control; 3rd Person camera (interactivity with environment); various player mechanics (run, walk, jump, slide, effects of slope angle on player).
Code Sample: Camera Control
The following is 3rd Person camera control; this checks if the camera is contacting or behind environment geometry. If so the script moves the camera smoothly into a desired position. Also, added "Debug" section for trouble shooting:
bool CheckIfOccluded(int count)
{
var isOccluded = false;
var nearDistance = CheckCameraPoints(TargetLookAt.position, desiredPosition);
if (nearDistance != -1)
{
if (count < MaxOcclusionChecks)
{
isOccluded = true;
Distance -= OcclusionDistanceStep;
if (Distance < 0.25f) //this will stop jittering of the camera: MAY NEED TO ADJUST DURING PLAY TEST
Distance = 0.25f;
}
else
Distance = nearDistance - Camera.mainCamera.nearClipPlane;
desiredDistance = Distance;
distanceSmooth = DistanceResumeSmooth;
}
return isOccluded;
}
float CheckCameraPoints(Vector3 from, Vector3 to)
{
var nearDistance = -1f;
RaycastHit hitInfo;
Helper.ClipPlanePoints clipPlanePoints = Helper.ClipPlaneAtNear(to);
//draw lines in the editor to make it easier to visualize
Debug.DrawLine(from, to + transform.forward * -camera.nearClipPlane, Color.red);
Debug.DrawLine(from, clipPlanePoints.UpperLeft);
Debug.DrawLine(from, clipPlanePoints.LowerLeft);
Debug.DrawLine(from, clipPlanePoints.UpperRight);
Debug.DrawLine(from, clipPlanePoints.LowerRight);
Debug.DrawLine(clipPlanePoints.UpperLeft, clipPlanePoints.UpperRight);
Debug.DrawLine(clipPlanePoints.UpperRight, clipPlanePoints.LowerRight);
Debug.DrawLine(clipPlanePoints.LowerRight, clipPlanePoints.LowerLeft);
Debug.DrawLine(clipPlanePoints.LowerLeft, clipPlanePoints.UpperLeft);
if(Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
nearDistance = hitInfo.distance;
if(Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
if (hitInfo.distance < nearDistance || nearDistance == -1)
nearDistance = hitInfo.distance
Scripts coded in C#. Scripts included: player input and animation control; 3rd Person camera (interactivity with environment); various player mechanics (run, walk, jump, slide, effects of slope angle on player).
Code Sample: Camera Control
The following is 3rd Person camera control; this checks if the camera is contacting or behind environment geometry. If so the script moves the camera smoothly into a desired position. Also, added "Debug" section for trouble shooting:
bool CheckIfOccluded(int count)
{
var isOccluded = false;
var nearDistance = CheckCameraPoints(TargetLookAt.position, desiredPosition);
if (nearDistance != -1)
{
if (count < MaxOcclusionChecks)
{
isOccluded = true;
Distance -= OcclusionDistanceStep;
if (Distance < 0.25f) //this will stop jittering of the camera: MAY NEED TO ADJUST DURING PLAY TEST
Distance = 0.25f;
}
else
Distance = nearDistance - Camera.mainCamera.nearClipPlane;
desiredDistance = Distance;
distanceSmooth = DistanceResumeSmooth;
}
return isOccluded;
}
float CheckCameraPoints(Vector3 from, Vector3 to)
{
var nearDistance = -1f;
RaycastHit hitInfo;
Helper.ClipPlanePoints clipPlanePoints = Helper.ClipPlaneAtNear(to);
//draw lines in the editor to make it easier to visualize
Debug.DrawLine(from, to + transform.forward * -camera.nearClipPlane, Color.red);
Debug.DrawLine(from, clipPlanePoints.UpperLeft);
Debug.DrawLine(from, clipPlanePoints.LowerLeft);
Debug.DrawLine(from, clipPlanePoints.UpperRight);
Debug.DrawLine(from, clipPlanePoints.LowerRight);
Debug.DrawLine(clipPlanePoints.UpperLeft, clipPlanePoints.UpperRight);
Debug.DrawLine(clipPlanePoints.UpperRight, clipPlanePoints.LowerRight);
Debug.DrawLine(clipPlanePoints.LowerRight, clipPlanePoints.LowerLeft);
Debug.DrawLine(clipPlanePoints.LowerLeft, clipPlanePoints.UpperLeft);
if(Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
nearDistance = hitInfo.distance;
if(Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
if (hitInfo.distance < nearDistance || nearDistance == -1)
nearDistance = hitInfo.distance
Code Sample: Player Physics - slide effect based on slope angle of environment objects
void ApplySlide()
{
if (!TP_Controller.CharacterController.isGrounded)
return;
slideDirection = Vector3.zero;
RaycastHit hitInfo;
if(Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hitInfo)) //save hit info in this and give it back to us
{
if (hitInfo.normal.y < SlideThreshold)
slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
}
if (slideDirection.magnitude < MaxControllableSlideMagnitude)
MoveVector += slideDirection;
else
{
MoveVector = slideDirection;
}
}
public void Jump()
{
if (TP_Controller.CharacterController.isGrounded)
VerticalVelocity = JumpSpeed;
}
void ApplySlide()
{
if (!TP_Controller.CharacterController.isGrounded)
return;
slideDirection = Vector3.zero;
RaycastHit hitInfo;
if(Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hitInfo)) //save hit info in this and give it back to us
{
if (hitInfo.normal.y < SlideThreshold)
slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
}
if (slideDirection.magnitude < MaxControllableSlideMagnitude)
MoveVector += slideDirection;
else
{
MoveVector = slideDirection;
}
}
public void Jump()
{
if (TP_Controller.CharacterController.isGrounded)
VerticalVelocity = JumpSpeed;
}