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 SpringCompress : MonoBehaviour
- {
- public Transform otherEnd; // The Other end of the spring that this object should try stay a set distance from
- public float targetDistance; // The distance this object should try keep from otherEnd
- public float springForce;
- private Rigidbody rigid;
- // Start is called before the first frame update
- void Start()
- {
- rigid = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- private float current = Vector3.Distance(transform.position, otherEnd.position);
- if(current > targetDistance) // Spring is extended
- {
- //Pull in spring
- transform.LookAt(otherEnd);
- rigid.AddRelativeForce(Vector3.forward * springForce);
- }
- else // spring is compressed
- {
- //extend Spring
- transform.LookAt(otherEnd);
- rigid.AddRelativeForce(Vector3.forward * -springForce);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement