Skip to content

Instantly share code, notes, and snippets.

View NickDiMucci's full-sized avatar

Nicholas DiMucci NickDiMucci

View GitHub Profile
private void obtainScreenBounds() {
float cameraToPlayerDistance = Mathf.Abs(Camera.mainCamera.transform.position.z - transform.position.z);
minScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, cameraToPlayerDistance));
maxScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, cameraToPlayerDistance));
}
private void checkScreenBoundaries() {
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, (minScreenBounds.x) + 1, (maxScreenBounds.x) - 1),
Mathf.Clamp(transform.position.y, (minScreenBounds.y) + 1, (maxScreenBounds.y) - 1),
@NickDiMucci
NickDiMucci / gist:5020984
Created February 23, 2013 19:28
A poor way to keep a player within the screen bounds in a Unity script.
player.transform.position = new Vector3(
Mathf.Clamp(player.transform.position.x, -8.0f, 8.0f),
Mathf.Clamp(player.transform.position.y, -10.0f, 10.0f),
player.transform.position.z);
@NickDiMucci
NickDiMucci / FlashPlayer.cs
Last active December 14, 2015 03:39
How I'm creating a color flash effect on the player when he gets hit by an object.
public Color[] flashColors;
private float flashLerpDuration = 0.10f;
private float flashInvokeDuration = 0.5f;
void Start() {
// All other initialization code here...
flashColors = new Color[2];
flashColors[0] = Color.magenta;
flashColors[1] = Color.white;
}
@NickDiMucci
NickDiMucci / Enemy.cs
Last active December 14, 2015 06:08
How to get around the inability to have constructor parameters in Unity
public EnemyBullet bullet;
private void fireBullet() {
EnemyBullet enemyBullet = Instantiate(bullet, gun.position, gun.rotation) as EnemyBullet;
enemyBullet.setSpeed(speed + BULLET_SPEED_MULTIPLIER);
laser.Play();
}
@NickDiMucci
NickDiMucci / EnemyBullet.cs
Created February 26, 2013 16:53
Setting the speed in Start
void Start() {
speed = 15.0f;
// Other code here...
}
@NickDiMucci
NickDiMucci / Enemy.cs
Last active December 14, 2015 06:38
Example showing GetComponent usage to access a prefab's script.
public Transform bullet;
private void fireBullet() {
Transform gameObject = Instantiate(bullet, gun.position, gun.rotation) as Transform;
gameObject.GetComponent<EnemyBullet>().setSpeed(speed + BULLET_SPEED_MULTIPLIER);
laser.Play();
}
@NickDiMucci
NickDiMucci / BasicEntityCollision.cs
Last active May 16, 2021 06:54
Basic collision detection via ray casting in Unity.
public class BasicEntityCollision : IEntityCollision {
private Vector3 size;
private Vector3 center;
// Give a bit of space between the raycast and boxCollider to prevent ray going through collision layer.
private float skin = .005f;
private LayerMask collisionMask;
private LayerMask playerMask;
public bool OnGround { get; set; }
@NickDiMucci
NickDiMucci / DefaultParamTest.cs
Created April 23, 2014 14:56
Unity default parameter method inside a namespace test. This currently doesn't work as of Unity 4.3.4f1. Multiple exceptions are thrown at runtime.
namespace com.mindshaft.test {
public class DefaultParamTest : MonoBehaviour {
void Update() {
DefaultParamTest();
}
private void DefaultParamTest(int param = 0) {
Debug.Log("param: " + param);
}
}
//Copyright (c) 2014 Tilman Schmidt (@KeyMaster_)
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class UnityBulkReplaceTool : MonoBehaviour
{
public GameObject objectToReplace = null;
public GameObject objectToSpawn = null;