Skip to content
Snippets Groups Projects
WorldCursor.cs 2.22 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Events;
    
    Richard Marcus's avatar
    Richard Marcus committed
    using UnityEngine.EventSystems;
    
    John Schihada's avatar
    John Schihada committed
    using static CommunicationEvents;
    
    
    public class WorldCursor : MonoBehaviour
    {
    
        public RaycastHit Hit;
    
    Richard Marcus's avatar
    Richard Marcus committed
    
    
    Richard Marcus's avatar
    Richard Marcus committed
    
    
            //Set MarkPointMode as the default ActiveToolMode
    
    John Schihada's avatar
    John Schihada committed
            ActiveToolMode = ToolMode.ExtraMode;//ToolMode.MarkPointMode;
            CommunicationEvents.ToolModeChangedEvent.Invoke(ActiveToolMode);
    
        }
    
        // Update is called once per frame
        void Update()
        {
            Ray ray = Cam.ScreenPointToRay(Input.mousePosition);
    
           
            int layerMask = 1 << LayerMask.NameToLayer("Player"); //only hit player
            layerMask = ~layerMask; //ignore Player
    
    
            if(Physics.Raycast(ray, out Hit, 30f, layerMask)){
                transform.position = Hit.point;
                transform.up = Hit.normal;
                transform.position += .01f * Hit.normal;
    
    Richard Marcus's avatar
    Richard Marcus committed
                transform.position = Cam.ScreenToWorldPoint(Input.mousePosition);
    
                transform.up = -Cam.transform.forward;
            }
    
    
            //Check if the ToolMode was switched
            CheckToolModeSelection();
    
        //Check if left Mouse-Button was pressed and handle it
        void CheckMouseButtons(Ray ray)
        {
    
                if (EventSystem.current.IsPointerOverGameObject()) return; //this prevents rays from shooting through ui
           
                CommunicationEvents.TriggerEvent.Invoke(Hit);
    
    John Schihada's avatar
    John Schihada committed
        //Checks if the ToolMode was switched by User, and handle it
    
        void CheckToolModeSelection() {
            if (Input.GetButtonDown("ToolMode")) {
                //Change the ActiveToolMode dependent on which Mode was selected
    
    John Schihada's avatar
    John Schihada committed
                if ((int)ActiveToolMode == Enum.GetNames(typeof(ToolMode)).Length - 1)
    
    John Schihada's avatar
    John Schihada committed
                    ActiveToolMode = 0;
    
    John Schihada's avatar
    John Schihada committed
                    ActiveToolMode++;
    
    John Schihada's avatar
    John Schihada committed
                CommunicationEvents.ToolModeChangedEvent.Invoke(ActiveToolMode);