Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class DoorDrag : MonoBehaviour {
- public GameObject playerCam;
- public float maxDistance = 4.0f;
- public string targetTag;
- public Transform grabber; // here's the needed "grab"-object target
- private Ray playerAim;
- private Vector3 currentTarget;
- private GameObject currentHitElement;
- private void RaycastProc() {
- playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
- RaycastHit hit;
- currentHitElement = null;
- Debug.DrawRay(playerAim.origin, playerAim.direction * maxDistance, Color.red, 0f, false);
- currentTarget = playerAim.origin + playerAim.direction * maxDistance; // endpoint of ray
- if (Physics.Raycast (playerAim, out hit, maxDistance)){
- if(hit.collider.gameObject.tag == targetTag) {
- currentTarget = hit.point; // hitpoint of ray, if found
- currentHitElement = hit.collider.gameObject;
- Debug.DrawRay(hit.point + new Vector3(-.3f, 0f, 0f), new Vector3(.6f, 0f, 0f), Color.green);
- Debug.DrawRay(hit.point + new Vector3(0f, -.3f, 0f), new Vector3(0f, .6f, 0f), Color.green);
- Debug.DrawRay(hit.point + new Vector3(0f, 0f, -.3f), new Vector3(0f, 0f, .6f), Color.green);
- }
- }
- }
- void LateUpdate() {
- RaycastProc();
- grabber.position = currentTarget;
- }
- void Update () {
- SpringJoint j = grabber.gameObject.GetComponent<SpringJoint>();
- if((Input.GetKeyDown(KeyCode.E) || Input.GetMouseButtonDown(0))) {
- if(currentHitElement != null) {
- if(j.connectedBody == null) {
- // connect to door
- } else {
- // free door
- }
- } else {
- // free door too
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement