• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SeahorsePro · Mar 17, 2020 at 04:28 PM · uiinputmodules

How do I "fake" the mouse position by overriding the base input class?

I have a world space canvass and can interact with a button on said canvass with my mouse. I will be locking the cursor though which disables said interaction. I've looked up some posts on creating custom input modules to replace the stand alone input module and am having a difficult time with it.


One solution was to override the mouse position in the BaseInput class and pass in a Vector2.zero. I have disabled the standalone input on my EventSystem GO and added the following scripts from another post.


using UnityEngine; using UnityEngine.EventSystems;

 [RequireComponent(typeof(MyBaseInput))]
 public class MyInputModule : StandaloneInputModule
 {
     protected override void Start()
     {
         this.inputOverride = GetComponent<MyBaseInput>();
         base.Start();
     }
 }


using UnityEngine; using UnityEngine.EventSystems;

 public class MyBaseInput : BaseInput
 {
     public Vector2 CursorPosition = Vector2.zero;
 
     public override Vector2 mousePosition
     {
         get { return this.CursorPosition; }
     }
 }


I am clearly missing a core concept here... in my mind this should be overriding the mouse position to the center of the screen. Am I completely off the mark here?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SeahorsePro · Mar 17, 2020 at 08:58 PM

Found a solution to my problem via this link http://talesfromtherift.com/vr-gaze-input/


Thanks to Peter Koch and his code I was able to determine that most of the functions I required were in the PointerInputModule and I could "fake" a mouse position with it's pointerEventData.position.


The following is the boiled down code I am using since I am not implementing a VR system and will continue to modify as I require a Vector3.Distance from my player to the worldspace buttons in question. Hope this helps someone else!


using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;

 public class WorldSpaceUIModule : PointerInputModule
     {
         public string ClickInputName = "Submit";
         public RaycastResult CurrentRaycast;
     
         private PointerEventData pointerEventData;
         private GameObject currentLookAtHandler;
     
         public override void Process()
         {
             SetPointerPosition();
             HandleRaycast();
             HandleSelection();
         }
     
         private void SetPointerPosition()
         {
             if (pointerEventData == null)
             {
                 pointerEventData = new PointerEventData(eventSystem);
             }
     
             // fake a pointer always being at the center of the screen
             pointerEventData.position = new Vector2(Screen.width / 2, Screen.height / 2);
             pointerEventData.delta = Vector2.zero;
         }
     
         private void HandleRaycast()
         {
             List<RaycastResult> raycastResults = new List<RaycastResult>();
             eventSystem.RaycastAll(pointerEventData, raycastResults);
             CurrentRaycast = pointerEventData.pointerCurrentRaycast = FindFirstRaycast(raycastResults);
     
             ProcessMove(pointerEventData);
         }
     
         private void HandleSelection()
         {
             if (pointerEventData.pointerEnter != null)
             {
                 GameObject handler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(pointerEventData.pointerEnter);
     
                 if (currentLookAtHandler != handler)
                 {
                     currentLookAtHandler = handler;
                 }
     
                 if (currentLookAtHandler != null && Input.GetMouseButtonDown(0))
                 {
                     ExecuteEvents.ExecuteHierarchy(currentLookAtHandler, pointerEventData, ExecuteEvents.pointerClickHandler);
                 }
             }
             else
             {
                 currentLookAtHandler = null;
             }
         }
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

220 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I change the input "submit" and "cancel" button of an eventsystem from a script? 0 Answers

is it Possible to use navigation on non UI elements? 1 Answer

How can I get text in a canvas to display the name of a key that is chosen by the player in the input panel of the launcher? 1 Answer

EventSystem events reaching parent EventTrigger? 0 Answers

Blocking Input.ButtonDown and Input.GetAxis when over UI,Block Input.GetButton when over an UI Element 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges