Guest User

ChainHandle.cs

a guest
Jun 23rd, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ChainHandle : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     Transform player;
  9.     [SerializeField]
  10.     Transform firstChain;
  11.     float distance;
  12.     // Start is called before the first frame update
  13.     void Start()
  14.     {          
  15.         distance = Vector3.Distance(transform.position, firstChain.position);      
  16.     }
  17.     void OnDrawGizmos()
  18.     {
  19.         Gizmos.color = Color.yellow;
  20.         if (firstChain != null)
  21.         {
  22.             Gizmos.DrawWireSphere(firstChain.position, distance) ;
  23.         }
  24.     }
  25.  
  26.     private void FixedUpdate()
  27.     {
  28.         // towards "player"
  29.             Vector3 dir = player.transform.position - firstChain.transform.position;
  30.         // clamp length
  31.             dir = Vector3.ClampMagnitude(dir, distance);
  32.         // add clamped length
  33.             transform.position = firstChain.transform.position + dir;
  34.         // look towards movement
  35.             transform.rotation = Quaternion.LookRotation(-dir);
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment