Last active
April 13, 2020 12:52
-
-
Save BanksySan/529b9a99256055f7a1d18504637a0002 to your computer and use it in GitHub Desktop.
Unity Scripts
This file contains hidden or 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; | |
using static UnityEngine.Debug; | |
[ExecuteInEditMode] | |
[RequireComponent(typeof(MeshFilter))] | |
public class DrawTriangleNormals : MonoBehaviour | |
{ | |
private Mesh _mesh; | |
private MeshFilter _meshFilter; | |
public Color _rayColor = Color.cyan; | |
private int[] _trianglesIndexes; | |
private Vector3[] _vertices; | |
private void Start() | |
{ | |
_meshFilter = GetComponent<MeshFilter>(); | |
_mesh = _meshFilter.sharedMesh; | |
_vertices = _mesh.vertices; | |
_trianglesIndexes = _mesh.triangles; | |
} | |
private void Update() | |
{ | |
for (var i = 0; i < _trianglesIndexes.Length; i += 3) | |
{ | |
var indexA = _trianglesIndexes[i]; | |
var indexB = _trianglesIndexes[i + 1]; | |
var indexC = _trianglesIndexes[i + 2]; | |
var vertexA = _vertices[indexA]; | |
var vertexB = _vertices[indexB]; | |
var vertexC = _vertices[indexC]; | |
var normal = Vector3.Cross(vertexB - vertexA, vertexC - vertexA); | |
var normalised = normal.normalized; | |
var centre = (vertexA + vertexB + vertexC) / 3; | |
DrawRay(transform.TransformPoint(centre), transform.TransformDirection(normalised), _rayColor, 0); | |
} | |
} | |
} |
This file contains hidden or 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; | |
using static UnityEngine.Debug; | |
[ExecuteInEditMode] | |
[RequireComponent(typeof(MeshFilter))] | |
public class DrawVertexNormals : MonoBehaviour | |
{ | |
private Mesh _mesh; | |
private MeshFilter _meshFilter; | |
private Vector3[] _vertices; | |
public Color _rayColor = Color.yellow; | |
private void Start() | |
{ | |
_meshFilter = GetComponent<MeshFilter>(); | |
_mesh = _meshFilter.sharedMesh; | |
_vertices = _mesh.vertices; | |
} | |
private void Update() | |
{ | |
_mesh.RecalculateNormals(); | |
var normals = _mesh.normals; | |
for (var i = 0; i < normals.Length; i++) | |
{ | |
DrawRay(transform.TransformPoint(_vertices[i]), transform.TransformDirection(normals[i]), _rayColor, 0, false); | |
} | |
} | |
} |
This file contains hidden or 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; | |
[RequireComponent(typeof(Rigidbody2D))] | |
public class PlayerController : MonoBehaviour | |
{ | |
private const float WALK_SPEED = 2; | |
private const float JUMP_FORCE = 250; | |
private Vector2 _jumpVector; | |
private bool _queueJump; | |
private bool _queueWalkLeft; | |
private bool _queueWalkRight; | |
private Rigidbody2D _rigidbody; | |
private void Start() | |
{ | |
_rigidbody = GetComponent<Rigidbody2D>(); | |
_jumpVector = new Vector2(0, JUMP_FORCE); | |
} | |
private void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.UpArrow)) _queueJump = true; | |
if (Input.GetKey(KeyCode.LeftArrow)) _queueWalkLeft = true; | |
if (Input.GetKey(KeyCode.RightArrow)) _queueWalkRight = true; | |
} | |
private void FixedUpdate() | |
{ | |
var velocity = _rigidbody.velocity; | |
if (_queueJump && _isGrounded) _rigidbody.AddForce(_jumpVector); | |
if (_queueWalkLeft == _queueWalkRight) | |
{ | |
velocity = new Vector2(0, _rigidbody.velocity.y); | |
} | |
else | |
{ | |
if (_queueWalkRight) velocity = new Vector2(WALK_SPEED, _rigidbody.velocity.y); | |
if (_queueWalkLeft) velocity = new Vector2(-WALK_SPEED, _rigidbody.velocity.y); | |
} | |
_rigidbody.velocity = velocity; | |
_queueJump | |
= _queueWalkLeft | |
= _queueWalkRight | |
= false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment