David Flynn
Create Your First Project
Start adding your projects to your portfolio. Click on "Manage Projects" to get started
Code Examples
Project type
Code
Here are examples of code I have written in C#.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Delivery : MonoBehaviour
{
[SerializeField] Color32 hasPackageColor = new Color32 (1,1,1,1);
[SerializeField] Color32 noPackageColor = new Color32 (0,1,0,1);
SpriteRenderer spriteRenderer;
void Start() {
spriteRenderer = GetComponent<SpriteRenderer>();
}
bool hasPackage;
[SerializeField] float destroyDelay = 0.5f;
void OnCollisionEnter2D(Collision2D other) {
Debug.Log("Impact");
}
private void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Package" && !hasPackage){
Debug.Log("Picked up Package");
hasPackage = true;
spriteRenderer.color = hasPackageColor;
Destroy(other.gameObject, destroyDelay);
}
if (other.tag == "Customer" && hasPackage){
Debug.Log("Package delivered");
hasPackage = false;
spriteRenderer.color = noPackageColor;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Driver : MonoBehaviour
{
[SerializeField] float steerSpeed = 1f;
[SerializeField] float moveSpeed = 20f;
[SerializeField] float slowSpeed = 15f;
[SerializeField] float boostSpeed = 30f;
// Start is called before the first frame update
void Start()
{
}
private void OnCollisionEnter2D(Collision2D other) {
moveSpeed = slowSpeed;
Debug.Log("SpeedDown");
}
void OnTriggerEnter2D(Collider2D other) {
if(other.tag == "Boost"){
Debug.Log("Speed Up");
moveSpeed = boostSpeed;
}
}
// Update is called once per frame
void Update()
{
float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;;
transform.Rotate(0, 0, -steerAmount);
transform.Translate(0, moveAmount, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCamera : MonoBehaviour
{
// Follows the Vehicle Object
[SerializeField] GameObject thingToFollow;
void LateUpdate()
{
transform.position = thingToFollow.transform.position + new Vector3(0,0, -10);
}
}