Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ChainLink : MonoBehaviour
- {
- public GameObject Head;
- //can also be calculated at start using and offset
- static float ChainSize = 1.6f;
- //So that the spawning script and other can use the size of the chains
- public static float GetChainSize()
- {
- return ChainSize;
- }
- private void FixedUpdate()
- {
- //When the link is hooked to something
- if (Head != null)
- {
- //We first get the offset and then break it into distance and direction
- Vector3 offset = this.transform.position - Head.transform.position;
- float distance = offset.magnitude;
- Vector3 direction = offset / distance;
- //Using the direction we place the link
- if (offset != Vector3.zero)//Unity will give a NaN error if offset is zero, it does nothing really
- {
- this.transform.position = Head.transform.position + (direction * ChainSize);
- this.transform.LookAt(Head.transform);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement