Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 VaSiLiZz · Jan 18, 2019 at 09:55 AM · mouse cursor

How to make mouse cursor move automatically onto a 3D object in the scene

Hi,

I have a scene whose only 3D object is a sphere and I want when pressing a key (say, Shift Key), the mouse cursor to move automatically onto the sphere. For that purpose, I've written the following piece of code but it only works for specific camera angles and positions and only when Unity Editor's 'Game' window is set to 'Maximize On Play':

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Runtime.InteropServices;
 using System;
 
 public class CursorMove: MonoBehaviour {
 
     public struct Point {
 
         public int X;
         public int Y;
 
         public Point (int _X, int _Y) {
             X = _X;
             Y = _Y;
         }
 
     }
 
     [DllImport("user32.dll")]
     private static extern bool SetCursorPos(int X, int Y);
     [DllImport("user32.dll")]
     private static extern bool GetCursorPos(out Point pos);
 
     public Transform target;
 
     private Point CurrentCursorPosition, NextCursorPosition;
 
     void Update() {
         
         if (Input.GetKeyUp(KeyCode.LeftShift)) {
 
             CurrentCursorPosition = new Point ();
             GetCursorPos (out CurrentCursorPosition);
 
             Vector3 Coords = GetComponent<Camera> ().WorldToViewportPoint (target.position);
 
                 Point Coords_AdjustedToResolution =
                     new Point ((int)(Coords.x * (float)Screen.currentResolution.width), 
                         (int)(Coords.y * (float)Screen.currentResolution.height));
 
             NextCursorPosition = 
                     new Point (Coords_AdjustedToResolution.X, Coords_AdjustedToResolution.Y);
             
             SetCursorPos (NextCursorPosition.X, NextCursorPosition.Y);
         
         }
 
     }
 
 }    

What should I do to make it work for all camera angles and positions?

Thanks in advance.

Comment
Add comment · Show 4
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
avatar image xxmariofer · Jan 18, 2019 at 10:16 AM 0
Share

Have you tried using https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html rather that to viewportpoint? cant test it but i think it might work since transform the point into the screen position so angles and positions should not be affected.

avatar image VaSiLiZz xxmariofer · Jan 18, 2019 at 10:30 AM 0
Share

Yes, I have tried WorldToScreenPoint too, but to no avail. In fact, it gives less accurate results than WorldToViewportPoint. I guess some extra calculations regarding the screen resolution need to be included.

avatar image Captain_Pineapple · Jan 18, 2019 at 12:02 PM 1
Share

Have you considered writing your own ingame cursor? Depending on the rest of your game setup this might be the simplest solution. Basic steps would be: - Lock the actual mouse at center and hide it. - Display a custom ui widget with your custom cursor. - move the widget by getting the mouse input - or set its onscreen position by simply giving it the ui intersection of a camera to target raycast.

avatar image VaSiLiZz Captain_Pineapple · Jan 18, 2019 at 01:46 PM 0
Share

I'll check that, too. Thanks.

1 Reply

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

Answer by VaSiLiZz · Apr 22, 2019 at 09:07 PM

I solved it! The only thing I needed to do is to change the assignment at line 39 into the following:

 Point ScreenCoords_AdjustedToResolution = new Point ((int)(ScreenCoords.x),
          (Screen.currentResolution.height - (int)(ScreenCoords.y)));

The subtraction from Screen's height takes places because the Y-coordinate in a Scene increases upwards, unlike Screen's Y-coordinate which increases downwards.

I should also mention that this method works only in full screen mode.

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.

Follow this Question

Answers Answers and Comments

99 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

Related Questions

Is possible to put a custom mouse texture farther than any near objects? 0 Answers

mouse cursor is not changing in browser 0 Answers

Error with mouse cursor. 1 Answer

Help with rotation! 0 Answers

mouse cursor not visible on VR 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