• 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 user-3204 (google) · Jun 25, 2010 at 09:47 PM · 2dscreenpointtoray

Trying to find how the 'y' position of a Raycast with ScreenPointToRay is set

Hello, I have a 2d game that is laying down in the x & z. You click and drag a game object across the screen. This work, the only problem I have is, when I get the 'hit.point' the 'y' that gets returns is extremely 'high'. I try and set it manually after but it glitches and jumps back and forth from the 'y' in the hit.point to the 'y' that i set manually. Any help at all would be greatly appreciated. Thanks Fabrizio

function Update () { if (Input.GetButton("Fire1")) { var ray: Ray = mCamera.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit;

     if (Physics.Raycast(ray, hit))
     {
         if(!rockCreated)
         {
             var rock: GameObject;
             var newPosition = Vector3(hit.point.x, hit.point.y, hit.point.z);
             rock = Instantiate(rockPrefab, newPosition, City.transform.rotation);
             Physics.IgnoreCollision(rock.collider, City.collider);
             currRock = rock;
         }
     }

     if(rockCreated)
     {
         Debug.Log("Y position of Hit" + hit.point.y);

         currRock.transform.position.x = hit.point.x;
         currRock.transform.position.z = hit.point.z;
         //currRock.transform.position.y = hit.point.y;
     }
 }

}

Comment
Add comment · Show 2
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 Peter G · Jun 25, 2010 at 09:51 PM 0
Share

It would help if we could see your ray variables (i.e. start, direction) and possibly have an idea of where your colliders are.

avatar image user-3204 (google) · Jun 28, 2010 at 01:45 PM 0
Share

Hi, sorry if i'm not sure exactly what you are referring to. Which 'start,direction' are you referring to? The Rock? Since it is instantiated, the start is where you click? Again, sorry if i don't understand exactly what you are asking. Thanks

1 Reply

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

Answer by Mike 3 · Jun 25, 2010 at 11:44 PM

The if(rockCreated) bit should really be inside the if for the raycast, otherwise you're checking against junk or null values, and setting the x/z every frame the mouse is down

If you do want to set the x/z every frame the mouse is down, regardless of hitting something, then store a vector3 when you hit, and set them using that vector instead - i'd skip using the hit variable for anything outside of the raycast check

Comment
Add comment · Show 3 · 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 user-3204 (google) · Jun 28, 2010 at 01:42 PM 0
Share

Thanks, I tried storing the hit in a Vector3 and it didn't seem to achieve what I was looking for. I think my biggest problem is that when the user clicks on the 'screen', the Y position returned is much higher than I need and that i'm expecting. Unless I misunderstand Raycast. When you click, how is it calculating the Y position (in this case at least)? Is it using the Camera position or something else? If I can figure out how that is calculated, then I can adjust it to where I need it to be, and I think I'll be in good shape. Thanks again for your help.

avatar image Mike 3 · Jun 28, 2010 at 02:10 PM 0
Share

The Y position should be the y position of whatever the ray hits. If it's hitting something too high, it's possible you've got a collider in the way somewhere

avatar image user-3204 (google) · Jun 28, 2010 at 02:31 PM 0
Share

Yup, that was the problem. Thanks so much for your help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2D Animation does not start 1 Answer

Unity 5 - 2d Instantiate at mouse position 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Limit user input until a player action is completed - Prince of Persia Classic 2D 1 Answer

making changes to 2dgameplay jumpfunctions 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