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 GluedBrain · Apr 27, 2015 at 01:26 PM · c#raycastlocalposition

Get Local Mouse Position from the center of the Gameobject clicked on

I want to get the mouse clicked position from the center of the gameobject, say a Sphere of Scale(1, 1, 1), for instance. If I click on the center of the sphere it should return the x component as zero, on clicking to the extreme left of the sphere, it should return -0.5 as the x component of the vector3 and 0.5 on clicking the extreme right of the sphere. The below code helps me achieve this. However, there is one constraint for it. The Sphere has to be positioned at (0, anything, anything) (as I am concerned with the x axis). Any help on how can I achieve this regardless of the Sphere position?

 bool isGameOver = false;
     float pointX;
 
     // Update is called once per frame
     void Update () {
         if(!isGameOver){
             RaycastHit hit;
             if(Input.GetMouseButtonDown(0)){
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if(Physics.Raycast(ray, out hit)){
                     if(hit.transform.tag =="Ball"){
                         pointX = transform.InverseTransformPoint(hit.point).x;
                     }
                 }
             }
         }
     }



EDIT: I am using Perspective camera, it works fine for Orthographic view of a camera

Comment
Add comment · Show 6
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 Chris333 · Apr 27, 2015 at 02:57 PM 0
Share

Hi,

i tried ur code and i positioned a cube at random positions where x != 0 and it gaves me the correct cords for x between -0.5 und 0.5 relative to the center point of the cube.

Did i understand something wrong?

avatar image GluedBrain · Apr 27, 2015 at 03:11 PM 0
Share

Hi, If I position it, say x=2. And I click on the center of the sphere, it doesn't give me 0. It gives me -0.15 kind of values.

avatar image GluedBrain · Apr 27, 2015 at 03:16 PM 0
Share

@Chris333.. I am using Perspective camera, it works fine for Orthographic view of a camera

avatar image Chris333 · Apr 27, 2015 at 03:25 PM 0
Share

Did you assigned the script to the sphere and not to the camera?

Iam using perspective too.

avatar image GluedBrain · Apr 27, 2015 at 03:38 PM 0
Share

i assigned script to sphere

Show more comments

2 Replies

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

Answer by Chris333 · Apr 27, 2015 at 05:39 PM

I tried it now with a sphere and it looks a like that it has something to do with the rotation. I get different values if i rotate the sphere.

Can you trie that instead of InverseTransformPoint().x

 if(hit.transform.tag =="Ball"){
     //pointX = transform.InverseTransformPoint(hit.point).x;
     Debug.Log (transform.position.x - hit.point.x);
 }
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
avatar image
1

Answer by fafase · Apr 27, 2015 at 07:30 PM

InverseTransformPoint converts a world point to a local point so obviously the rotation will affect the result.

If you are facing me and I throw a ball at you, you will get it at (0,0,1), now if you turn 90 degrees and I throw again, I will hit you at (1,0,0). Even though the position in the world space is the same, in your own system it has changed.

So your solution is indeed to remain in world space with some basic vector arithmetic as shown above.

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

How to have an object some distance above another object Locally? 1 Answer

Multiple Cars not working 1 Answer

Locking gameobject to rotating floor 0 Answers

Raycast - arrow stopped by walls (layer) but not by units (layer) 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