Thursday, 11 August 2011

Assignment Final


I have finished checking the game for any flaws. The controls are working fine. I helped with the creation of the controls and contributed to the overall design of the game.

This was the final script for the movement of the car:


public class PlayerControl : MonoBehaviour
{
    public float playerSpeed = 100;
    public float bankSpeed = 0.2f;
public float speed = 10.0f;
public float jumpForce = 10.0f;
public float gravityForce = 20.0f;
    public int bankAngle = 70;
    public CharacterController controller;

(These are the functions defined at the top. Each have a subsequent value due to the valuables below)


private Vector3 dir;
    private Vector3 moveDirection = Vector3.zero;
    private float expSpeed;
    //public static float pSpeed;

    // Use this for initialization
    void Start()
    {
       //publish playerSpeed via pSpeed
       //pSpeed = playerSpeed;

       // this.expSpeed = this.playerSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        #region Move the player
        //Get the  left and right arrow keys to control the x direction of player
        float horMov = Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;

(This tells the program to use the left and right keys and control the movement of the car in the X direction)

        // Get the up and down arrow keys to control the y direction of player
        float verMov = Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime;

(This tells the program to use the left and right keys and control the movement of the car in the Y direction)

        //exponitially increase speed
        this.expSpeed = (this.expSpeed * 1.01f);

(This increases the speed of the car linearly per second)

        // move the player left and right
        transform.Translate(Vector3.right * (horMov+expSpeed));

        //move the forward and backward
        transform.Translate(Vector3.back * -verMov);

(This moves the car left and right)
        #endregion
//clamp the player movement
this.transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, -30f, 50f),
                                     this.transform.position.y,
 Mathf.Clamp(this.transform.position.z, -220f, -170f));

(This limits the movement of the car so that it will not go beyond a certain limit. The clamping ensures that when the car moves left or right, it will only more beyond a certain point)

        #region bank the player

        // Set rotation to local Angles
        Vector3 rotation = this.transform.localEulerAngles;

        //Set Velocity
        float velocity = 0.0F;

        // set the rotation on z to the input of the verticle buttons  using smoothDampAngle to:
        // Slowly rotate  when button is pressed and then return to orginal angle when button is released
        rotation.z = Mathf.SmoothDampAngle(rotation.z, -Input.GetAxis("Horizontal") * bankAngle, ref velocity, bankSpeed);
        transform.localEulerAngles = rotation;



        #endregion

        #region jump
//apply gravity
        dir.y = controller.velocity.y - gravityForce * Time.deltaTime;

//jump
         if (Input.GetKeyUp("space"))
        {
            dir.y = jumpForce * gravityForce * Time.deltaTime;
        }

        #endregion


    }