Skip to content

Instantly share code, notes, and snippets.

@CreedsCode
Last active July 30, 2018 01:42
Show Gist options
  • Save CreedsCode/e7958ad1cc627fe5bae5a1a60904ff15 to your computer and use it in GitHub Desktop.
Save CreedsCode/e7958ad1cc627fe5bae5a1a60904ff15 to your computer and use it in GitHub Desktop.
Unity3D Script snippets/examples
//Copied From https://gist.github.com/unity3dcollege/b43888d4d17bef0c21369d6cc32352dd
using UnityEngine;
public class BallBouncer : MonoBehaviour
{
[SerializeField]
[Tooltip("Just for debugging, adds some velocity during OnEnable")]
private Vector3 initialVelocity;
[SerializeField]
private float minVelocity = 10f;
private Vector3 lastFrameVelocity;
private Rigidbody rb;
private void OnEnable()
{
rb = GetComponent<Rigidbody>();
rb.velocity = initialVelocity;
}
private void Update()
{
lastFrameVelocity = rb.velocity;
}
private void OnCollisionEnter(Collision collision)
{
Bounce(collision.contacts[0].normal);
}
private void Bounce(Vector3 collisionNormal)
{
var speed = lastFrameVelocity.magnitude;
var direction = Vector3.Reflect(lastFrameVelocity.normalized, collisionNormal);
Debug.Log("Out Direction: " + direction);
rb.velocity = direction * Mathf.Max(speed, minVelocity);
}
}
using UnityEngine;
public class PositionScript : MonoBehaviour {
public int x, y, z;
void Update () {
transform.Translate(x * Time.deltaTime, y * Time.deltaTime, z * Time.deltaTime);
}
}
using UnityEngine;
public class RotationScript : MonoBehaviour {
public int x, y, z;
void Update()
{
transform.Rotate(x * Time.deltaTime, y * Time.deltaTime, z * Time.deltaTime);
}
}
using UnityEngine;
public class SpawnScript : MonoBehaviour {
public GameObject Object,SpawnPoint;
void Start()
{
Spawn();
}
void Spawn () {
Instantiate(Object, SpawnPoint.transform.position, Quaternion.identity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment