Advertisement
Guest User

Untitled

a guest
Nov 18th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class pickupController : MonoBehaviour
  6. {
  7.     bool canPickUp;
  8.     bool carrying = false;
  9.     GameObject player;
  10.     GameObject obj;
  11.     RaycastHit hit;
  12.     Ray ray;
  13.     [SerializeField]
  14.     float range = 3f;
  15.     bool holoActive = false;
  16.  
  17.  
  18.     // Start is called before the first frame update
  19.     void Start()
  20.     {
  21.         obj = this.transform.parent.gameObject;
  22.         canPickUp = false;
  23.         player = GameObject.FindGameObjectWithTag("Player");
  24.     }
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.  
  30.         if(canPickUp == true)
  31.         {
  32.             PickUp();          
  33.         }
  34.  
  35.         if(carrying == true)
  36.         {
  37.  
  38.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  39.             if (Physics.Raycast(ray, out hit))
  40.             {
  41.                 if (hit.transform.tag == "Surface" && Vector3.Distance(player.transform.position, hit.point) < range)
  42.                 {
  43.                     obj.transform.Find("holo").gameObject.SetActive(true);
  44.                     obj.transform.position = new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z);
  45.                 }
  46.             }
  47.  
  48.         }
  49.                
  50.  
  51.     }
  52.  
  53.     private void OnTriggerEnter(UnityEngine.Collider other)
  54.     {
  55.        if (other.transform.tag == "Player")
  56.         {
  57.             canPickUp = true;
  58.        }
  59.     }
  60.  
  61.     private void OnTriggerExit(UnityEngine.Collider other)
  62.     {
  63.         if(other.transform.tag == "Player")
  64.         {
  65.             canPickUp = false;
  66.         }
  67.        
  68.     }
  69.  
  70.     void PickUp()
  71.     {
  72.         canPickUp = false;
  73.         carrying = true;
  74.         obj.transform.GetComponent<Collider>().enabled = false;
  75.         obj.transform.GetComponent<Rigidbody>().isKinematic = true;
  76.         foreach (MeshRenderer mesh in obj.transform.GetComponentsInChildren<MeshRenderer>())
  77.         {
  78.             if (mesh.gameObject.name != "holo")
  79.             {
  80.                 mesh.enabled = false;
  81.             }
  82.  
  83.         }
  84.     }
  85.  
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement