9 December 2014

Scripting II


After reading that all scripts should be posted in full, I have decided to return to the scripts I produced for this group project, and post them verbatim here as text, instead of screenshots. I make no promises that this is the fastest, cleanest or most efficient way To achieve what I set out to do, but it makes sense to me and it works.

Scripting after the break,



#pragma strict

public var speed : float = 10f;
// Sets speed of rotation

function Update ()
{
    transform.Rotate(Vector3.up, speed * Time.deltaTime);
    //rotates gameobject script it attached to along the Y axis
}

A small script that allows a transparent hemisphere to rotate, simulating moving clouds.

#pragma strict

// place in world raft objects will be moved to
var raftplacement : GameObject;

var raftgrid_1 : GameObject;
var raftcable_1 : GameObject;
var raftcable_2: GameObject;
var raftbox_1 : GameObject;
var raftmast_1 : GameObject;
var raftsail_1 : GameObject;
var raftbarrel_1 : GameObject;
var raftbarrel_2 : GameObject;
var raftbarrel_3 : GameObject;
var raftbarrel_4 : GameObject;

//checks collider player collided with

function OnTriggerEnter(Col : Collider)
{
  if(Col.gameObject.tag == "RaftGrid")
  { 
  Raftgrid_1.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftCable")
  { 
  raftcable_1.renderer.enabled == false;
  raftcable_2.renderer.enabled == true;
  }
  if(Col.gameObject.tag == "RaftBox")
 { 
  raftbox_1.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftMast")
  { 
  raftmast_1.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftSail")
  { 
  raftsail_1.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftBarrel_1")
  { 
  raftbarrel_1.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftBarrel_2")
  { 
  raftbarrel_2.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftBarrel_3")
  { 
  raftbarrel_3.transform.position = raftplacement.transform.position;
  }
  if(Col.gameObject.tag == "RaftBarrel_4")
  { 
  raftbarrel_4.transform.position = raftplacement.transform.position;
  }
}
A probably overly-complicated script dealing with moving raft objects around the map, assembling the final raft in a spot on the game scene.

 
  var Alight : GameObject;
  var count : boolean;

    InvokeRepeating("flash", 2, 1);

function flash ()
    {
    count = !count;

        if (count == true)
            {
                Alight.renderer.material.color = Color.red;
            }
            else
                {
                    Alight.renderer.material.color = Color.black;
                }

    }

A script that uses InvokeRepeating to flip a light between on an off every second, InvokeRepeating was used instead of FunctionUpdate as it's less expensive in runtime.

#pragma strict

var waterlevel : GameObject;
var floatheight : float = 2;
var bouncedamp : float = 0.05;
var bouyancycentreoffset : Vector3;

private var forcefactor : float;
private var actionpoint : Vector3;
private var uplift : Vector3;

function Update ()
{
actionpoint = transform.position + transform.TransformDirection(bouyancycentreoffset);
forcefactor = 1f - ((actionpoint.y - waterlevel.transform.position.y) / floatheight);

if(floatheight > 0f)
{
uplift = -Physics.gravity * ( forcefactor - rigidbody.velocity.y * bouncedamp);
rigidbody.AddForceAtPosition(uplift, actionpoint);
}
}

A script that modifies an objects Y co-ordinate in such a way to simulate bobbing up and down in water.

 private var timer = 0.0; 
 var bobbingSpeed = 0.18; 
 var bobbingAmount = 0.2; 
 var midpoint = 2.0; 

 function Update () { 
    waveslice = 0.0; 
    horizontal = Input.GetAxis("Horizontal"); 
    vertical = Input.GetAxis("Vertical"); 
    if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) { 
       timer = 0.0; 
    } 
    else { 
       waveslice = Mathf.Sin(timer); 
       timer = timer + bobbingSpeed; 
       if (timer > Mathf.PI * 2) { 
          timer = timer - (Mathf.PI * 2); 
       } 
    } 
    if (waveslice != 0) { 
       translateChange = waveslice * bobbingAmount; 
       totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical); 
       totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0); 
       translateChange = totalAxes * translateChange; 
       transform.localPosition.y = midpoint + translateChange; 
    } 
    else { 
       transform.localPosition.y = midpoint; 
    } 
 }

A script that moves the camera up and down to simulate view bobbing.

#pragma strict

var vertigo : Camera;
var player : GameObject;

function Update ()
{
var hit : RaycastHit;

if (Physics.Raycast (transform.position, transform.forward, hit, 100) && hit.distance >10)
{
Debug.DrawLine (transform.position, hit.point, Color.cyan);
if (hit.transform.position.y < player.transform.position.y && vertigo.fieldOfView <100)
{
vertigo.fieldOfView ++;
}
}

else
{
if (vertigo.fieldOfView >70)
{
vertigo.fieldOfView --;
}
}
}

A script that creates a raycast at the player, and if it hits an object within one hundred units , and is more than ten units below the player in the Y co-ordinate, pulls the camera FoV back to simulate vertigo. This links in with our mechanic of fall damage, as  if the player were to fall ten or more units they would be moved back to the start of the map.

#pragma strict

// Holds last position of the player on the Y axis, for calculating fall distance.
var LastPositionY : float =0f;
//Holds Distance player has fallen
var fallDistance : float = 0f;
//Var for player regarding Ladders
var player : Transform;
// Timer for Fade In
var timer : float = 3.3;
// Tells Ladder Script when Player is inside Ladder Collision  Box
var inside : boolean = false;
// Related to speed player travels up a ladder
var heightFactor : float = 3.2;
// Plane for initial Fade In
var plane : GameObject;
//spawn location
var destination : GameObject;

var Splash : AudioSource;
var Hit : AudioSource;

private var FPSInput : FPSInputController;
private var controller : CharacterController;

function Start()
{
plane.renderer.material.color.a = 1;
controller = GameObject.Find("First Person Controller").GetComponent (CharacterController);
FPSInput = GetComponent(FPSInputController);
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = false;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
GameObject.Find("Main Camera").GetComponent(MouseLook).enabled = false;
}

// Runs when Character falls more than 10 metres
function CharacterDeath()
{
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = false;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
GameObject.Find("Main Camera").GetComponent(MouseLook).enabled = false;
plane.renderer.material.color.a = 1;
player.transform.position = destination.transform.position;
yield WaitForSeconds(1);
timer = 2;
}
//Runs when character hits water plane
function WaterDeath()
{
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = false;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
GameObject.Find("Main Camera").GetComponent(MouseLook).enabled = false;
plane.renderer.material.color.a = 1;
player.transform.position = destination.transform.position;
yield WaitForSeconds(1);
timer = 2;
}

function OnTriggerEnter(Col : Collider)
//swaps value of inside variable for ladders on entering a ladder collider
{
if(Col.gameObject.tag == "Ladder")
{
inside = !inside;
}
//kills player on collision with water plane
if(Col.collider.tag == "WaterPlane")
{
Splash.Play();
WaterDeath();
}
}
// resets inside variable for ladder colliders
function OnTriggerExit(Col : Collider)
{
if(Col.gameObject.tag == "Ladder")
{
inside = !inside;
}
}

//resets fall distance if less than 10 metres
function ApplyNormal()
{
fallDistance = 0;
LastPositionY = 0;
}

function Update()
{
if(LastPositionY > player.transform.position.y)
{
fallDistance += LastPositionY - player.transform.position.y;
}
LastPositionY = player.transform.position.y;

// kills player on falls greater than ten metres
if(fallDistance <=10 && controller.isGrounded)
{
ApplyNormal();
}

if(fallDistance >=10 && controller.isGrounded)
{ Hit.Play();
CharacterDeath();
ApplyNormal();
}

//resets fall damage when inside a ladder collider
if(inside == true)
{
ApplyNormal();
}
// allows player to climb ladder

if(inside == true && Input.GetKey("w"))

player.transform.position += Vector3.up / heightFactor;
}

//Handles the fade in of the scene on start and respawn
timer = timer - Time.deltaTime;

if (timer >= 0)
{
plane.renderer.material.color.a -= 0.3 * Time.deltaTime;
}

if(timer <= 0)
{
timer = 0;
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = true;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
GameObject.Find("Main Camera").GetComponent(MouseLook).enabled = true;
}

if(player.position.y <= 4)
{
WaterDeath();
}
}

The main player script, which is an amalgamation of numerous functions, such as fading in and out of the scene at the start and after a character Death, the character deaths themselves, ladders and fall damage. I don't know the benefits to having all of these functions tied to one single script opposed to many scripts working on one single function, but this script has arisen in such a way as I am still unsure how to get separate scripts to link and interact with eachother