Advertisement
MysteryGM

Unity_CustomBasicIK

Jun 23rd, 2020
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ChainLink : MonoBehaviour
  6. {
  7.     public GameObject Head;
  8.  
  9.     //can also be calculated at start using and offset
  10.     static float ChainSize = 1.6f;
  11.  
  12.     //So that the spawning script and other can use the size of the chains
  13.     public static float GetChainSize()
  14.     {
  15.         return ChainSize;
  16.     }
  17.  
  18.     private void FixedUpdate()
  19.     {
  20.         //When the link is hooked to something
  21.         if (Head != null)
  22.         {
  23.             //We first get the offset and then break it into distance and direction
  24.             Vector3 offset = this.transform.position - Head.transform.position;
  25.             float distance = offset.magnitude;
  26.             Vector3 direction = offset / distance;
  27.  
  28.             //Using the direction we place the link
  29.             if (offset != Vector3.zero)//Unity will give a NaN error if offset is zero, it does nothing really
  30.             {
  31.                 this.transform.position = Head.transform.position + (direction * ChainSize);
  32.                 this.transform.LookAt(Head.transform);
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement