Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bls61793 · Feb 02, 2014 at 01:35 AM · c#bug

Trying to make gameobject follow mouse cursor. It disappears instead.

So, after a long time of not flexing any sort of programming/ game-dev mucle, I decided that today I would try to make a very simple game in Unity to warm myself up a little inside.

 Problem is, I am trying to do something very simple, with the simple script below, and it is not working. Also I do not know why.
 
 The following is a monobehavior script in C#  placed on a sprie game object. Which is currently the only game object on screen.




 using UnityEngine;
 using System.Collections;
 
 public class FollowMouseScript : MonoBehaviour {
     public Vector3 UltimatePosition = new Vector3(0,0,0);
     float X, Y;
 
 
 
     // Use this for initialization
     void Start () {
 
     
     }
     
     // Update is called once per frame
     void Update () {
 
         X = Input.mousePosition.x;
         Y = Input.mousePosition.y;
         Vector3 mouselocation = Camera.main.camera.ScreenToWorldPoint(Input.mousePosition);
         transform.position = mouselocation;
         //print("MouseX: " + X + " MouseY: " + Y);
 
         //print(transform.position.x + "," + transform.position.y + "," + transform.position.z);
         if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)  == true) {
             print("click");
                 }
 
     }
 }

The problem is, both of my print statements to the console tell me that the transform.position(s) of the object are being properly set. However, the object appears to just disappear. I have checked the z values of the camera and the object (I'm working in 2D mode) and the camera defaults to -5 and the sprite defaults to 0).

Any suggestions/help?

EDIT: Upon some further reading and some editing, I have now accounted for the difference in screenspace and world space using Camera.main.camera.ScreenToWorldPoint. The issue remains. I can post my altered code if you guys wish.

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
6

Answer by robertbu · Feb 02, 2014 at 02:33 AM

Yes, edit your question and replace your code above with the latest. Note when setting up the position you will pass to ScreenToWorldPoint(), the 'z' must be set to the distance in front of the camera. Your code here is setting it to transform.position.z. For a camera without rotation and moving objects parallel to the camera plane, you can calculate the 'z' paramter as:

 Vector3 pos = Input.mousePosition;
 pos.z = transform.position.z - Camera.main.transform.position.z;
 transform.position = Camera.main.ScreenToWorldPoint(pos);
Comment
Add comment · Show 5 · 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 bls61793 · Feb 02, 2014 at 03:33 AM 0
Share

Thanks, That worked, but I'm not exactly sure I understand why changing the z value of the vector to the difference worked. Could someone talk a shot at reexplaining it. Sorry I didn't get it the first time, but I want to understand why this worked. Thanks

P.S. : Thanks again.

avatar image robertbu · Feb 02, 2014 at 03:52 AM 0
Share

What you are looking for is the positive distance between the plane of the camera and the plane you are moving the objects. For example, say your object have a 'z' value of 0, and the camera has a 'z' value of -10. So the distance would be 10. The calc above:

 transform.position.z - Camera.main.transform.position.z 

comes down to:

    0 - (-10) = 10
avatar image bls61793 · Feb 02, 2014 at 05:37 AM 0
Share

What I mean is though, before the calculation, I would be sending in a value to Camera.main.ScreenToWorldPoint of (mousex, mousey, and 0) why does sending in 10 ins$$anonymous$$d of 0 for the z value allow me to see the object. Or Rather, why does a z value of 0 going into the ScreenToWorld point function make me unable to see my sprite?

avatar image Funlunde · Aug 29, 2016 at 06:18 AM 0
Share

Thank you.

avatar image Shippety · Mar 18, 2017 at 04:43 PM 0
Share

I was having same issues- thanks!

avatar image
0

Answer by LangNoob · Mar 24, 2016 at 09:42 PM

Thanks for posting these replies. It really helped me out.

@bls61793 Although this thread is pretty old, I wasn't sure if you got the answer your question about why you can't see your sprites when the camera z is set to 0. In case you haven't and you come back to this for whatever reason, the answer is because with the camera and sprite being on the same z plane, if both are set to 0, the camera is too close to see the sprite. You add the distance of 10 to push the camera further back from the sprite which is why you are now able to see it.

Thanks again!

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Line Renderer Positioning Problem 1 Answer

Help in solving bug in my code :) 1 Answer

Spawn with rotation of 90 on x axis. 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