• 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 oliver-jones · May 19, 2012 at 07:43 PM · raycastfloatinfinitylimitation

Raycast - Input position is Infinity | Point Precision

Hello,

I'm trying to get an object to follow my mouse position within the scene, and to do that I'm using ScreenPointToRay. When I run the scene, the object doesn't follow my mouse, and I get a warning saying: Input position is { Infinity, -1...... }.

Also, on the actual object thats suppose to be following my mouse, I get a little warning displaying under the Transform in my Inspector window saying: Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range

What does any of this mean, and how would I go about fixing it?

Here is my script:

 // -- place control -- \\
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 
 Physics.Raycast(ray, hit, Mathf.Infinity);
 var positionForm = Vector3(hit.point.x, transform.position.y, hit.point.z) - transform.position;
 transform.Translate(positionForm);

Thanks

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 flaminghairball · May 19, 2012 at 08:13 PM

Hm… I notice a couple of things in your code.

1) transform.Translate moves your object by the vector you input - not to it. Just call transform.position = positionForm; to move it to the hit point. 2) You're not testing if the ray cast hits something. Physics.Raycast returns a boolean that indicates whether the raycast actually hit anything. I'm not sure what hit.point is set to if the ray cast doesn't hit anything, but I'd imagine based on the error you're seeing, it's infinity. :p 3) Floats have to fit into a certain number of bytes, and so they lose precision as the number they represent gets larger. Since you're trying to place an object at such a high value, the floats are imprecise(perhaps someone more intelligent than me could expand on this oversimplification).

 if(Physics.Raycast(ray, hit, Mathf.Infinity){
     transform.position = Vector3(hit.point.x, transform.position.y, hit.point.z);
 } else {
     transform.position = ray.GetPoint(10f); //returns a point ten units out along the ray
 }
 
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i change a variable on the object hit by a raycast C# 2 Answers

What's the most efficient raycast distance, and why? 2 Answers

Assignment of float is incorrectly set to zero. 2 Answers

How can i modify a float in another script in C#? 1 Answer

[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges