mvaganov

ForcePull.cs (for VRTK v3)

Dec 9th, 2018
106
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. public class ForcePull : MonoBehaviour {
  6.     public float grabRadius = .25f;
  7.     public float grabForce = 5;
  8.     public float grabRange = 100;
  9.     public float emitRate = 16;
  10.     // VRTK.VRTK_ControllerEvents cEv;
  11.     VRTK.VRTK_InteractGrab grabber;
  12.     VRTK.VRTK_InteractUse user;
  13.     void Start () {
  14.         // cEv = GetComponent<VRTK.VRTK_ControllerEvents> ();
  15.         grabber = GetComponent<VRTK.VRTK_InteractGrab> ();
  16.         if (grabber == null) { grabber = gameObject.AddComponent<VRTK.VRTK_InteractGrab> (); }
  17.         user = GetComponent<VRTK.VRTK_InteractUse> ();
  18.         if (user == null) { user = gameObject.AddComponent<VRTK.VRTK_InteractUse> (); }
  19.         emitParams.startSize = 1;
  20.         emitParams.startLifetime = 1;
  21.         emitParams.startColor = telekenisisParticle.main.startColor.color;
  22.     }
  23.  
  24.     GameObject line_ray, line_target, line_hit;
  25.     public ParticleSystem telekenisisParticle;
  26.     private ParticleSystem.EmitParams emitParams = new ParticleSystem.EmitParams();
  27.  
  28.     bool hadGravity;
  29.     Rigidbody rbBeingMovedWithTheForce;
  30.  
  31.     Rigidbody GetRigidbodyFrom(GameObject go) {
  32.         Rigidbody rb = go.GetComponent<Rigidbody> ();
  33.         if (rb == null) {
  34.             rb = go.GetComponentInParent<Rigidbody> ();
  35.         }
  36.         return rb;
  37.     }
  38.  
  39.     float timer;
  40.     // Update is called once per frame
  41.     void Update () {
  42.         timer -= Time.deltaTime;
  43.         Rigidbody rbBeingPointedAt = null;
  44.         Vector3 raydir = -transform.right;
  45.         if (user.IsUseButtonPressed() && !grabber.IsGrabButtonPressed ()) {
  46.             Vector3 camDir = transform.forward;
  47.             if (Camera.main != null) {
  48.                 camDir = Camera.main.transform.forward;
  49.             }
  50.             float alignment = Vector3.Dot (raydir, camDir);
  51.     //      NS.Lines.MakeArrow (ref line_ray, transform.position, transform.position + raydir, Color.gray);
  52.             if (alignment > .5f) {
  53.                 RaycastHit rh = new RaycastHit ();
  54.                 Vector3 rayStart = transform.position - raydir * grabRadius;
  55.                 // get all the objects in range
  56.                 RaycastHit[] hits = Physics.SphereCastAll (rayStart, grabRadius, raydir, grabRange);
  57. //              if (Physics.SphereCast (rayStart, grabRadius, raydir, out rh)) {
  58.                 Vector3 emitFrom = rayStart + raydir * grabRange;;
  59.                 if(hits != null) {
  60.     //          NS.Lines.MakeCircle (ref line_hit, rh.point, rh.normal, Color.black, grabRadius);
  61.     //          telekenisisParticle.transform.position = rh.point;
  62.                     // select the one that is the most well aligned, and closest
  63.                     int bestIndex = -1;
  64.                     float bestScore = float.PositiveInfinity;
  65.                     for (int i = 0; i < hits.Length; ++i) {
  66.                         Rigidbody rb = GetRigidbodyFrom (hits [i].collider.gameObject);
  67.                         if (rb != null) {
  68.                             Vector3 delta = hits [i].point - rayStart;
  69.                             float dist = delta.magnitude;
  70.                             float align = (1 - Vector3.Dot (raydir, delta / dist));
  71.                             float score = dist * align * align;
  72.                             if(bestIndex < 0 || score < bestScore){
  73.                                 bestScore = score;
  74.                                 bestIndex = i;
  75.                                 rbBeingPointedAt = rb;
  76.                                 emitFrom = hits [i].point;
  77.                             }
  78.                         }
  79.                     }
  80.                     // if none are selected, show where the hand is pointing
  81.                     if (timer < 0) {
  82.                         if (rbBeingPointedAt == null) {
  83.                             if (Physics.Raycast (rayStart, raydir, out rh, grabRange)) {
  84.                                 emitFrom = rh.point;
  85.                             }
  86.                         }
  87.                         if(telekenisisParticle != null) {
  88.                             //telekenisisParticle.Emit (emitFrom, -raydir * 1, 1, 1, telekenisisParticle.main.startColor.color);
  89.                             emitParams.position = rh.point;
  90.                             emitParams.velocity = -raydir;
  91.                             telekenisisParticle.Emit(emitParams, 1);
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         if (rbBeingPointedAt != rbBeingMovedWithTheForce) {
  98.             if (rbBeingMovedWithTheForce != null) {
  99.                 if (hadGravity) {
  100.                     rbBeingMovedWithTheForce.useGravity = true;
  101.                 }
  102.                 rbBeingMovedWithTheForce = null;
  103.             }
  104.             if (rbBeingPointedAt == null) {
  105.                 //Debug.Log ("Stopped acting on " + rbBeingMovedWithTheForce);
  106.             }
  107.             rbBeingMovedWithTheForce = rbBeingPointedAt;
  108.             if (rbBeingMovedWithTheForce != null) {
  109.                 hadGravity = rbBeingMovedWithTheForce.useGravity;
  110.                 rbBeingMovedWithTheForce.useGravity = false;
  111.                 //Debug.Log ("Acting on " + rbBeingMovedWithTheForce);
  112.             }
  113.         }
  114.         float speed = 1;
  115.         if (rbBeingMovedWithTheForce != null) {
  116.             //NS.Lines.MakeArrow (ref line_ray, transform.position, acting.transform.position, Color.gray);
  117.             //NS.Lines.MakeCircle(ref line_target, acting.transform.position, raydir, Color.gray, grabRadius);
  118.             //telekenisisParticle.transform.position = rbBeingMovedWithTheForce.transform.position;
  119.             Vector3 delta = transform.position - rbBeingMovedWithTheForce.position;
  120.             float dist = delta.magnitude;
  121.             //Vector3 dir = delta / dist;
  122.             if (dist > 1) {
  123.                 speed = dist;
  124.             }
  125.             Vector3 idealV = -raydir * speed;
  126.             Vector3 accel = idealV - rbBeingMovedWithTheForce.velocity;
  127.             rbBeingMovedWithTheForce.velocity += accel.normalized * grabForce * Time.deltaTime;
  128.             if (timer < 0) {
  129.                 // telekenisisParticle.Emit (transform.position, raydir * speed, 1, 1, telekenisisParticle.main.startColor.color);
  130.                 emitParams.position = transform.position;
  131.                 emitParams.velocity = raydir * speed;
  132.                 telekenisisParticle.Emit(emitParams, 1);
  133.             }
  134.         }
  135.         if (timer < 0) {
  136.             timer = 1/emitRate;
  137.         }
  138.     }
  139. }
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.

×