-1

I have and raycast, and a rayCastHit. Whenever the user click on the fire button. It will move the FPS charater to the location where the rayCastHit is. lightRetical is a gameObject variable which is a spotlight that shows where the rayCastHit is.

The Funny thing is, it works when I click play in unity. But whenever I build to my android phone it doesn't work. I am unable to move the fps character.

The FPS character I used, is from the standard asset "character" and the codes I add them to the Update() method.

        RaycastHit seen;
        Ray raydirection = new Ray(transform.position, cam.transform.forward);
        int sightlength = 5;
        if (Physics.Raycast(raydirection, out seen, sightlength))
        {
            if (seen.collider.tag == "Floor" && Input.GetButtonDown("Fire1")) //in the editor, tag anything you want to interact with and use it here
            {
                Vector3 relativePoint;
                lightRetical.SetActive(true);
                relativePoint = seen.point;
                relativePoint.y = 2.0f;
                bodychar.transform.position = relativePoint;
            }
            else
            {
                lightRetical.SetActive(true);
                Vector3 relativePoint;
                relativePoint = seen.point;
                relativePoint.y = 2.64f;
                lightRetical.transform.position = relativePoint;

            }
        }
        else
        {
            lightRetical.SetActive(false);
        }
UserBlanko
  • 202
  • 2
  • 12
  • You have `ScreenPointToRay(Input.mousePosition);` I haven't used VR before but I think that might be the problem. That line of code, are you using am mouse or a touch sensor with VR? – Programmer Apr 11 '16 at 08:45
  • Thanks for replying, I'm not using anything else, just the VR alone. Do you have any suggestion that I can do? I am currently trying to replace the Input.mousePosition with new Vector3(200, 200, 0) – UserBlanko Apr 11 '16 at 08:48
  • I don't know why you would use `Input.mousePosition` if there is no mouse connected. Can you explain what you are trying to do in that line and in the second Raycast. Maybe there is another better way of doing this in VR. – Programmer Apr 11 '16 at 08:51
  • Oh come to think of it why did I used two raycast.... , the first raycast was suppose to show where the rayCastHit and the second raycast was suppose to move the character to the position. – UserBlanko Apr 11 '16 at 09:02
  • You can get the position from the first raycast and move the character instead of doing this twice... – Programmer Apr 11 '16 at 09:04
  • Okay, I found something useful I think I'll try this first http://forum.unity3d.com/threads/unity-5-2-vr-raycast-solved.361211/ Thanks anyway. – UserBlanko Apr 11 '16 at 09:13
  • @Programmer I changed it and it still doesn't work the light rectical doesn't show and I am unable to move the character – UserBlanko Apr 12 '16 at 07:06
  • What works and what doesn't. How about you put a `Debug.Log` in the raycast if statement and inside the button press if statement and tell me which one displayed. Also, in `Physics.Raycast(raydirection, out seen, sightlength)`, remove `sightlength` from that function for now. Add it when things start working. This is to make sure that distance is not the problem. – Programmer Apr 13 '16 at 09:11

1 Answers1

0

I suggest casting the ray from the camera position forward. If the player rotates his head the raycast will follow. I'm currently developing an app for VR and this seems like the best solution. You can use collision layers to filter the raycast. I would also print the hit.transform to the console, to check what the raycast is hitting. Hope this helps.

vonis22
  • 55
  • 7