- Home /
Why is the camera.screenpointtoray off?
There arent any errors that prevent me from playing, and it seems to work, but the ray is really off. I have found that I have to move the camera 90 degrees to the right for it to allow me to pick it up. Any suggestions?
#pragma strict
function Start () {
}
var guiray : GUIText;
var flashlight : GameObject;
var flashlightdesk : GameObject;
function Update () {
var hit : RaycastHit;
var ray = camera.ScreenPointToRay (Vector3(200,200,0));
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(ray, hit, 10)){
// print ("There is something in front of the object!");
// var fingerCount = 0;
// for (var touch : Touch in Input.touches) {
// if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
// fingerCount++;
// }
// if (fingerCount > 0){
// print ("User has " + fingerCount + " finger(s) touching the screen");
if(hit.collider.gameObject.CompareTag("light")) {
guiray.text = "Pick Up Flashlight";
// if (Input.touchCount > 0) {
if (Input.GetButtonDown("Fire1")){
flashlight.gameObject.active = true;
//flashlight.gameObject.enabled = true;
flashlightdesk.gameObject.active = false;
}
}
else {
guiray.text = "";
}
}
}
Please propery indent your code, it's looks horrible right now :)
Answer by robertbu · Feb 18, 2013 at 03:55 AM
As @Benproductions1 mentioned, please clean up your code before posting it to the list. You are far more likely to get answers if the code is readable. With that said, I pasted your code into a file and made some changes so it would run. When I examined the raycast, it was exactly as I expected. Perhaps you need to take a screen shot of the scene view so that we can see what you are seeing. The one major change I made to the code was to explicitly use Camera.main rather than the camera attached to the game object. Do you have multiple camera in your project? Here is the code I tested:
//var guiray : GUIText;
//var flashlight : GameObject;
//var flashlightdesk : GameObject;
function Update () {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Vector3(200,200,0));
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(ray, hit, 10)){
if(hit.collider.gameObject.CompareTag("light")) {
//guiray.text = "Pick Up Flashlight";
if (Input.GetButtonDown("Fire1")){
//flashlight.gameObject.active = true;
//flashlightdesk.gameObject.active = false;
}
}
else
{
//guiray.text = "";
}
}
}