Last active
July 30, 2018 01:42
-
-
Save CreedsCode/e7958ad1cc627fe5bae5a1a60904ff15 to your computer and use it in GitHub Desktop.
Unity3D Script snippets/examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class PositionScript : MonoBehaviour { | |
public int x, y, z; | |
void Update () { | |
transform.Translate(x * Time.deltaTime, y * Time.deltaTime, z * Time.deltaTime); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class RotationScript : MonoBehaviour { | |
public int x, y, z; | |
void Update() | |
{ | |
transform.Rotate(x * Time.deltaTime, y * Time.deltaTime, z * Time.deltaTime); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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