Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerMovement : MonoBehaviour {
  7.  
  8.     public Interactable focus;
  9.  
  10.     // Use this for initialization
  11.     void Start () {
  12.        
  13.     }
  14.    
  15.     // Update is called once per frame
  16.     void Update () {
  17.        
  18.         if (Input.GetButton("Fire1"))
  19.         {
  20.            //create a ray
  21.             Ray ray = Camera.main.ViewportPointToRay(Vector3.one / 2f);
  22.             Debug.DrawRay(ray.origin, ray.direction * 2f, Color.red);
  23.  
  24.             RaycastHit hitInfo;
  25.  
  26.             //If the ray hits
  27.             if(Physics.Raycast(ray, out hitInfo, 2f))
  28.             {
  29.                 Interactable interactable = hitInfo.collider.GetComponent<Interactable>();
  30.              
  31.                 //Check if we hit Interactable
  32.                 if (interactable != null)
  33.                 {
  34.                     SetFocus(interactable);
  35.                 }
  36.                
  37.             }
  38.         }
  39.         else
  40.         {
  41.             RemoveFocus();
  42.         }
  43.  
  44.     }
  45.  
  46.  
  47.  
  48.     private void SetFocus(Interactable newFocus)
  49.     {
  50.         focus = newFocus;
  51.     }
  52.     private void RemoveFocus()
  53.     {
  54.         focus = null;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement