- Home /
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.
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))
@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)
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
Answer by Will748 · Aug 07, 2012 at 01:16 PM
]!
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
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.
Your answer
Follow this Question
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