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
1
Question by Sir Mate · Sep 13, 2012 at 07:46 PM · transformvector3mousescreentoworldpoint

Mouse-following script freezes Unity.

Hey there.

So i wanted to make a system where an object would be used to move a weapon around. For that i need the object to be in sync with the mouse movement , and the weapon will use that object as a pointer. So i got this code together:

 #pragma strict
 
 private var temp : Vector3;
 
 function Update () {
 while (Input.GetMouseButton(0))
 {
 temp = Input.mousePosition;
 temp.z = gameObject.transform.position.z -Camera.main.transform.position.z;
 transform.position = Camera.main.ScreenToWorldPoint(temp.normalized);
 }
  
 }

I can see that it more or less works , as the objects coordinates are changing in synch with cursor movement, BUT!

1: If i removed the `while (Input.GetMouseButton(0))` , the object just... disappeared. it still must be in visible coordinates range ,so i have no idea what whichcraft is going on here, but moving the cursor does make the coordinates shift.

2: When i leave `while (Input.GetMouseButton(0))` in , upon pressing the LMB Unity freezes, it just locks up.

I am really kind of baffled about whats going on here ,so any help would be greatly appriciated.

Thanks!

EDIT: Thanks to ThermalFusion, the freeze problem was solved. But our job does not end there, as now a new problem emerged. Now i could see that the box was just warped to the cameras z coordinate for some reason, even though that the line

 temp.z = gameObject.transform.position.z -Camera.main.transform.position.z;

should have fixed that. I added +60 to the end of that line ,so i could see some results ,and the x and y coordinates sync up perfectly, whereas the object seems to be going on an ellipsoid-shaped round around me. where it is only like 20 units sfrom me on one side ,and at the opposite ,its like +30000000 or something. We still need to find the witch.

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 ThermalFusion · Sep 13, 2012 at 10:36 PM

Try using temp instead of temp.normalized:

 transform.position = Camera.main.ScreenToWorldPoint(temp);

while loops the code inside the curly brackets until its conditions are no longer true as Input.GetMouseButton(0) never gets set to false inside there, it will keep looping forever and Unity will crash. You want to use if here instead:

 if (Input.GetMouseButton(0))

Then it will only do the code once. And because it is inside the Update function, it will do what's inside Update once every frame.

 temp.z = gameObject.transform.position.z -Camera.main.transform.position.z;
 temp.z = 25f; // use this instead

You should probably change the line above to the exact distance from the camera you want the object to be placed.

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 Kaili-chan · Sep 13, 2012 at 10:52 PM 0
Share

TL;DR Change while to if

avatar image Sir Mate · Sep 14, 2012 at 12:28 PM 0
Share

Thank you ,thats one obsticle down! But a new one needs tackling.

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

11 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

Related Questions

Cannot conver UnityEngine.Transform to UnityEngine.GameObject 3 Answers

Make an Object Animate Up In The Update Function 2 Answers

Object Not Moving - Yield - IEnumerator 1 Answer

Input.mousePosition returns coordinates as integers. 1 Answer

GameObject.FindGameObjectWithTag is not working 2 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