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 /
  • Help Room /
avatar image
0
Question by StrangeDew · Aug 12, 2019 at 03:19 PM · mousepositionscreentoworldpoint

Object is stuck at center of camera instead of following mouse

I have been trying to make my game object follow the cursor but it's moving with the center of the camera instead. In my game, the camera follows the player as they move. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CrosshairBehaviour : MonoBehaviour
 {
     private void Update()
     {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.position = new Vector3(mousePos.x, mousePos.y, 0f);
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by I_Am_Err00r · Aug 12, 2019 at 06:08 PM

Hey there, I like your handle by the way @StrangeDew !


Is this script attached to just your crosshair? Is your player gameObject a parent of this crosshair? Maybe you just need to add a GameObject variable to your code to define what the cross hair is.


So I never use the mouse position personally, but I just tested this code and it worked exactly as you would probably want your code to (you need to add your crosshair prefab in the inspector for it to work):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Crosshair: MonoBehaviour
 {
     public GameObject crossHair;

     void FixedUpdate()
     {
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         crossHair.transform.position = mousePos;
     }
 }

I noticed you have the z axis set to 0, just use a Vector2 for mousePos and you don't need to worry about that z axis; I couldn't tell you exactly why your code wasn't working, but I got mine to work so that your mouse position moves the cross-hair.


Someone else with more coding knowledge might be able to definitively tell you when to use Update vs FixedUpdate, but I tend to make anything that handles movement of objects in FixedUpdate, and generally save Update for input handling and things like that (it has to do with framerates and from what I understand FixedUpdate is a better universal solution for movement handling because of the stability in the frames it's checking, where Update checks based on framerate and can cause stuttering at fluctuating rates, here is an answer on it), just best practice.

Comment
Add comment · Show 2 · 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
avatar image StrangeDew · Aug 21, 2019 at 05:06 PM 0
Share

I figured it out. I needed to set the z-axis to -10f. Apparently, ScreenToWorldPoint() takes into account the distance of the plane from the camera in the world.

avatar image I_Am_Err00r StrangeDew · Aug 21, 2019 at 05:13 PM 0
Share

I believe if you use a Vector2 like I had in the example it would fix that issue, but I could be wrong.


If you could humor me and try that, I'm curious (I'm at work and don't have access to Unity, but if you don't respond I will just test out when I'm home; I just want to make a mental note).

avatar image
0

Answer by AMMARx · May 24 at 08:58 PM

I know I'm late but I have been threw this problem until I figured out how to resolve it:

Just change the camera projection to Orthographic

@StrangeDew

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

180 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

Related Questions

Screen to World Point on 2.5D game 0 Answers

2.5D look at mouse 1 Answer

Move object to mouse position 1 Answer

ScreenToWorldPoint offset affecting ScreenPointToRay ? 1 Answer

Trying to make pointer, but ScreenToWorldPoint wont work 0 Answers


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