Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerClamped : MonoBehaviour
- {
- private Rigidbody rb;
- private Vector3 moveDirection;
- private float moveSpeed = 4f;
- public static float distance;
- [SerializeField]
- Transform firstChain;
- bool grabbed;
- // Use this for initialization
- public bool inRange;
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
- {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- }
- else
- {
- moveDirection = Vector3.zero;
- }
- if (Input.GetKeyDown(KeyCode.E) && inRange)
- {
- Grab();
- }
- if (moveDirection != Vector3.zero)
- {
- transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), 0.15F);
- moveDirection = new Vector3(moveDirection.x * moveSpeed, Mathf.Clamp(rb.velocity.y, -5, 5), moveDirection.z * moveSpeed);
- rb.velocity = moveDirection;
- if (grabbed)
- {
- // towards "player"
- Vector3 dir = transform.position - firstChain.transform.position;
- // clamp length
- dir = Vector3.ClampMagnitude(dir, distance);
- // add clamped length
- transform.position = firstChain.transform.position + dir;
- // look towards movement
- transform.rotation = Quaternion.LookRotation(-dir);
- }
- }
- else
- {
- rb.velocity = new Vector3(moveDirection.x, Mathf.Clamp(rb.velocity.y, -5, 5), moveDirection.z);
- }
- }
- void Grab()
- {
- grabbed = !grabbed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment