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 Will748 · Aug 07, 2012 at 11:23 AM · raycastscreentoworldpoint

Raycast, ScreenToWorldPoint accuracy problem

 var prefabGrapple:Transform;
 var distance = 10.0;
 var distanceFromCamera = 20;

 function Update ()
 {
  var player = GameObject.FindWithTag("Player");
  //on mouse click ...
  if(Input.GetButtonDown("Fire1"))
  {
  //Movement.gravity = 0.0;
  var mousePosition = Input.mousePosition;
  mousePosition.z = distanceFromCamera;
  var worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
  var hit : RaycastHit;
  Debug.Log(worldMousePosition);
 
  if(Physics.Raycast(transform.position, worldMousePosition, hit, distance))
  {
  var InstanceGrapple = Instantiate(prefabGrapple, hit.point, Quaternion.identity); 
  Debug.Log(worldMousePosition);
  player.rigidbody.constraints &=   ~RigidbodyConstraints.FreezeRotationZ;
  Movement.hasShot = true;
  Debug.Log("goodbye");
  }
  }

The problem is is that the direction doesn't seem to be in the direction of the mouse, the mouse does change the direction but is innacurate, can anyone see if there is anything in my code that is causing this innacuracy ? because ive been looking for ages but cant find out what it is.

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

4 Replies

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

Answer by FlyingOstriche · Aug 07, 2012 at 12:03 PM

 if(Physics.Raycast(transform.position, worldMousePosition, hit, distance))

worldMousePosition needs to be a direction, you could try.

 if(Physics.Raycast(transform.position, worldMousePosition-transform.position, hit, distance))
Comment
Add comment · Show 1 · 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 aldonaletto · Aug 07, 2012 at 12:24 PM 1
Share

@FlyingOstriche is right, you need a direction vector, and world$$anonymous$$ousePosition is a point. A better approach would be to use ScreenPointToRay ins$$anonymous$$d:

// declare player outside Update: private var player: GameObject;

function Update () { // find the player only once! if (!player) player = GameObject.FindWithTag("Player"); //on mouse click ... if(Input.GetButtonDown("Fire1")) { //$$anonymous$$ovement.gravity = 0.0; var ray = Camera.$$anonymous$$ain.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast(ray, hit, distance)) { var InstanceGrapple = Instantiate(prefabGrapple, hit.point, Quaternion.identity); player.rigidbody.constraints &= ~RigidbodyConstraints.FreezeRotationZ; $$anonymous$$ovement.hasShot = true; } } } Notice that the player variable was declared outside Update, and assigned only once in Update - this has nothing to do with the raycast, but improves performance (Find methods are too slow to be called every Update)

avatar image
0

Answer by drak0 · Aug 07, 2012 at 12:25 PM

Try this, see if it works:

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition)
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
// check if you hit the player and instantiate what you want if so

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
0

Answer by Will748 · Aug 07, 2012 at 01:16 PM

![alt text]!

alt text

Here is an image showing the problem, the sphere represents the mouse, the line is the raycast, what I thought would happen is that the line should intercept the sphere, however as you can see its off


untitled.png (18.6 kB)
untitled.png (19.2 kB)
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
0

Answer by drak0 · Aug 07, 2012 at 02:15 PM

I might be wrong but I think it has to do with one of the axis.Because Y should represent up and down; from what I see, a positive value on Z would mean away from the cube. My point being that the value of X affects your aiming.Try setting it at 0, see what happens.

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

10 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

Related Questions

Find TextureCoordinates from plane with out raycast only using mouse position on plan 0 Answers

How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow? 2 Answers

Camera.main.ScreenToWorldPoint not outputting expected results. 1 Answer

Camera.ScreenToWorldPoint with Perspective camera 1 Answer

ScreenToWorldPoint has some sort of offset 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