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 Stonemove420 · Mar 26, 2012 at 06:36 PM · positioncursor

Getting Cursor position problem

Hi guys, i want to instantiate a tower at my cursor's location when i click the tower's button in the menu. My problem is that when i do a debug.log on my cursor's position, it looks like 400,0,40 ( arround i dont remember the exact number). My point is , the tower does get instantiated but at a very far position... How can i solve this to get the "real" cursor position ? Thanks again guys !

So here's my code to do that

 void Update () 
 {
     mousePos.x = Input.mousePosition.x;
     mousePos.y = floorYpos;
     mousePos.z = Input.mousePosition.z;
     
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if(Physics.Raycast(ray,out hit, Mathf.Infinity))
     {
         if (hit.transform.tag == "Guardian")
         {
             if(Input.GetMouseButtonDown(0))
             {
                 checkIfClicked = true; // pops the menu over my "guardian"
             }
         }
     }
     
     if (summoninMonkey == true)
     {
         Instantiate(monkey,mousePos,transform.rotation);
         Debug.Log("its working");
         summoninMonkey = false;
     }
 }
 
 void OnGUI ()
 {

     if(checkIfClicked == true)
     {
         
         GUILayout.BeginArea(new Rect(350,25,120,120));
         
         if (GUILayout.Button("Summon Monkey", GUILayout.Width(120), GUILayout.Height(30)))
         {
             summoninMonkey = true;
             Debug.Log("going to change cursor for tower");
         }
         
         if (GUILayout.Button("Close Menu"))
         {
             Debug.Log("Closing menu");
             checkIfClicked = false;
         }
         GUILayout.EndArea();

     }    
 }
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
0
Best Answer

Answer by glipzcom · Mar 26, 2012 at 10:53 PM

I am realllly new to Unity, but I was trying to deal with this same problem today. Maybe I can help a bit. You want to instatiate at the point where that ray you cast collided with your object, not at the mouse position.

So you do that Raycast in the if statement. You are going to want to use the value of hit

Physics.Raycast(ray,out hit, Mathf.Infinity)

You can now set a Vector3 to the location where the hit occurred:

Vector3 newVector3 = ray.GetPoint (hit) ;

Then use that value of newVector3 in your instantiate:

Instantiate(monkey, newVector3 ,transform.rotation);

I am not sure if there are other problems you are having, but that might get you closer. Also I found this debug feature pretty helpful to visualize the ray:

Debug.DrawRay (ray.origin, ray.direction * 100, Color.yellow);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Disable/enable script and animation when you move your mouse cursor 1 Answer

Instantiate a prefab at the Mouse's position? 1 Answer

Weapon Possition Move On Sprint 1 Answer

addition of vector3 1 Answer

Positioning a GameObject so I't wont be inside another object 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