opponent019

CollisionScript

May 2nd, 2018
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // To be used with Melting Shader: https://pastebin.com/f6nKeerE
  6. // Created by Erika Moya, in collaboration with Gijs Richard Kaerts
  7.  
  8. public class CollisionScript : MonoBehaviour {
  9.    
  10.     Mesh mesh;
  11.     public float distance = 0.5f;
  12.     Vector3 lastHit = new Vector3 (0,0,0);
  13.     public float heightOffset = 0.0f;
  14.     float lastHeightOffset = 0.0f;
  15.  
  16.     private void Start()  {
  17.         mesh = GetComponent<MeshFilter>().mesh;
  18.     }
  19.  
  20.     void FixedUpdate() {
  21.         RaycastHit hit;
  22.         Ray ray = new Ray (transform.position, Vector3.down);
  23.         Physics.Raycast (ray, out hit);
  24.  
  25.         if (lastHit == new Vector3(0,0,0) ) {
  26.             if (hit.distance < distance) {
  27.                 SendMatrix (hit, lastHit);
  28.                 lastHit = hit.point;
  29.             }
  30.         }
  31.  
  32.         if ((hit.point != new Vector3(0,0,0)) && (lastHeightOffset != heightOffset)) {
  33.             lastHit = hit.point + heightOffset*(hit.normal);
  34.             lastHeightOffset = heightOffset;
  35.             SendMatrix (hit, lastHit);
  36.         }
  37.        
  38.     }
  39.  
  40.     void SendMatrix (RaycastHit hit, Vector3 newPoint) {
  41.         //Debug.Log("Collision detected");
  42.         Vector3 normal = hit.normal;
  43.         Vector3 tangent = Vector3.Cross(Vector3.forward, normal);
  44.         if (tangent.magnitude == 0) {
  45.             tangent = Vector3.Cross(Vector3.up, normal); }
  46.         Vector3 bitangent = Vector3.Cross(normal, tangent);
  47.  
  48.         Vector3 point = hit.point;
  49.         if (newPoint != new Vector3 (0,0,0)) {
  50.             point = newPoint;
  51.         }
  52.  
  53.         lastHit = point;
  54.  
  55.         Matrix4x4 colMatrix = new Matrix4x4();
  56.  
  57.         colMatrix.SetRow(0, new Vector4(tangent.x, tangent.y, tangent.z, 0));
  58.         colMatrix.SetRow(1, new Vector4 (normal.x, normal.y, normal.z, 0));
  59.         colMatrix.SetRow(2, new Vector4(bitangent.x, bitangent.y, bitangent.z, 0));
  60.         colMatrix.SetRow(3, new Vector4(point.x, point.y, point.z, 1));
  61.  
  62.         GetComponent<Renderer>().material.SetMatrix("collisionMatrix", colMatrix);
  63.  
  64.         GetComponent<Renderer>().material.SetMatrix("collisionMatrixInverse", colMatrix.inverse);
  65.         //Debug.Log("Matrix sent\n" + colMatrix.GetRow(3));
  66.     }
  67.        
  68. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×