Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // To be used with Melting Shader: https://pastebin.com/f6nKeerE
- // Created by Erika Moya, in collaboration with Gijs Richard Kaerts
- public class CollisionScript : MonoBehaviour {
- Mesh mesh;
- public float distance = 0.5f;
- Vector3 lastHit = new Vector3 (0,0,0);
- public float heightOffset = 0.0f;
- float lastHeightOffset = 0.0f;
- private void Start() {
- mesh = GetComponent<MeshFilter>().mesh;
- }
- void FixedUpdate() {
- RaycastHit hit;
- Ray ray = new Ray (transform.position, Vector3.down);
- Physics.Raycast (ray, out hit);
- if (lastHit == new Vector3(0,0,0) ) {
- if (hit.distance < distance) {
- SendMatrix (hit, lastHit);
- lastHit = hit.point;
- }
- }
- if ((hit.point != new Vector3(0,0,0)) && (lastHeightOffset != heightOffset)) {
- lastHit = hit.point + heightOffset*(hit.normal);
- lastHeightOffset = heightOffset;
- SendMatrix (hit, lastHit);
- }
- }
- void SendMatrix (RaycastHit hit, Vector3 newPoint) {
- //Debug.Log("Collision detected");
- Vector3 normal = hit.normal;
- Vector3 tangent = Vector3.Cross(Vector3.forward, normal);
- if (tangent.magnitude == 0) {
- tangent = Vector3.Cross(Vector3.up, normal); }
- Vector3 bitangent = Vector3.Cross(normal, tangent);
- Vector3 point = hit.point;
- if (newPoint != new Vector3 (0,0,0)) {
- point = newPoint;
- }
- lastHit = point;
- Matrix4x4 colMatrix = new Matrix4x4();
- colMatrix.SetRow(0, new Vector4(tangent.x, tangent.y, tangent.z, 0));
- colMatrix.SetRow(1, new Vector4 (normal.x, normal.y, normal.z, 0));
- colMatrix.SetRow(2, new Vector4(bitangent.x, bitangent.y, bitangent.z, 0));
- colMatrix.SetRow(3, new Vector4(point.x, point.y, point.z, 1));
- GetComponent<Renderer>().material.SetMatrix("collisionMatrix", colMatrix);
- GetComponent<Renderer>().material.SetMatrix("collisionMatrixInverse", colMatrix.inverse);
- //Debug.Log("Matrix sent\n" + colMatrix.GetRow(3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement