caden brown
Aug 29, 2017 · 3 min read

Here is the source code for my ball roller main script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class BallRoller : MonoBehaviour {

public float rollSpeed = 1; // Rolling speed of the ball
public float jumpSpeed = 10; // The jump speed of the ball
public float expandTime = 5; // The time it takes for the ball to expand
public int scoreCount; // The score of the player
public Vector3 ballScale = new Vector3(3, 3, 3); // How big to scale the ball when we expand
public Transform cameraTransform; // The transform of the camera
public Text text; // The GUI score text
private bool expand; // Should the ball expand
private Rigidbody rb; // Variable for our rigidbody

void Start() {

// Set our rb variable to our rigibbody component
rb = GetComponent<Rigidbody>();

// Set our score to 0
scoreCount = 0;

// Set our score text to our score count
SetScoreText();

}

// If we collide with another collider
private void OnCollisionEnter(Collision collision) {

// If the gameobject we collide with has the tag "BugBody"
if (collision.gameObject.tag == "BugBody" && collision.relativeVelocity.magnitude > 2) {

// Get the bugAi component from the object we hit
BugAI bugAI = collision.gameObject.GetComponentInParent<BugAI>();

// If the bug is not null
if (bugAI != null) {

// Minus health from the bug based off how hard we hit it
bugAI.health -= collision.relativeVelocity.magnitude;
}


}


}

private void Update() {

// If the "e" key is pressed
if (Input.GetKeyDown("e")) {

// Reload our current scene
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}

void FixedUpdate() {

// Variable that is equal to our horizontal axis
float moveHorizontal = Input.GetAxis("Horizontal");

// Variable that is equal to our vertical axis
float moveVertical = Input.GetAxis("Vertical");

// If the "f" key is pressed
if (Input.GetKeyDown("f")) {

// Set expand to the opposite of its current value if its true it sets it false and if its false it sets it true
expand = !expand;
}
// If expand is true
if (expand) {

// Lerp our scale to the ball scale variable
gameObject.transform.localScale = Vector3.Lerp(gameObject.transform.localScale, ballScale, Time.deltaTime * expandTime);
} else { // If its not true

// Lerp our scale back to 1
gameObject.transform.localScale = Vector3.Lerp(gameObject.transform.localScale, Vector3.one, Time.deltaTime * expandTime);
}

// Make a new vector3 movement thats x equals
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

// Rotatating the movement by the cameras rotation
movement = Quaternion.Euler(0, cameraTransform.rotation.eulerAngles.y, 0) * movement;

// Adds a force to the ball's rigidbody component based on the movement and speed
rb.AddForce(movement * rollSpeed);

// If space is pressed and we are on the ground
if (Input.GetKeyDown("space") && CheckGrounded()) {

// Add a force up so we jump
rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
}

}
// Bool for checking if we are grounded
private bool CheckGrounded() {

// If we raycast down .6 meters from the balls center
if (Physics.Raycast(transform.position, Vector3.down, 0.6f)) {
// If it hits set check grounded to true
return true;
} else { // If it doesn't hit anything
// Set grounded to false
return false;
}

}
// If we enter a trigger
private void OnTriggerEnter(Collider other) {

// Log "Hit!" to the console
Debug.Log("Hit!");
}
// Function for setting our GUI text to our score count
public void SetScoreText() {

// Sets the text to Score + our current score count
text.text = "Score:" + scoreCount.ToString();
}

}
)