- Home /
C# Camera.main.ScreenPointToRay on android
I need to convert this from Javacript to C#, but it doesn't seem to work...
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast (ray, hit, 100))
{
if (! selectedCard)
{
DoSomething();
}
}
}
Works perfectly on both Windows AND Android.
But when i convert this to C#, it only works on Windows.... Android Input's are not recognized...
Why is that? What do i have to do to get this working?
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
if (! selectedCard){
DoSomething();
}
}
}
You need to put the type of variable before ray, which is Ray:
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
You will need to do the same for the RaycastHit variable wherever you have declared that:
RaycastHit hit;
I don't see the rest of your code, but if this is happening in update it has to be:
void Update()
not:
function Update()
It's not a syntax error, the code compiles fine, and it works on the Windows build... but as i build for android and transfer the AP$$anonymous$$ to my device, it doesn't respond to any input.
I've declared both ray and hit in the class scope:
using UnityEngine;
using System.Collections;
public class cardSelection : $$anonymous$$onoBehaviour {
private RaycastHit hit;
private Ray ray;
private GameObject matchOne;
private GameObject matchTwo;
private bool touchEnabled = true;
void Update () {
if (touchEnabled) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Touch / $$anonymous$$ousecontrol
if (Input.Get$$anonymous$$ouseButtonDown (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
if (! matchOne)
{
RevealCardOne();
}
else if (matchOne && !matchTwo)
{
StartCoroutine (RevealCardTwo());
if (cardsLeft == 0)
{
GameWon();
}
}
}
}
}
What i have also tried (which doesn't work either):
if (Input.touches[0].phase == TouchPhase.Began || Input.touches[0].phase == TouchPhase.Stationary || Input.touches[0].phase == TouchPhase.$$anonymous$$oved) {
ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
if (Physics.Raycast (ray, out hit, 100))
{
if (! matchOne)
{
RevealCardOne();
}
else if (matchOne && !matchTwo)
{
StartCoroutine (RevealCardTwo());
if (cardsLeft == 0)
{
GameWon();
}
}
}
}
Answer by Talantyyr · Aug 10, 2013 at 06:16 PM
Ok... My Fault...
I had two scripts with the same name (.cs and .js) because i've copied it once to rewrite it in C#...
When compiling for android, i've got several warnings with Unable to properly serialize object because of extra field ....
I've removed the duplicate script and everything works now... stupid me -.-
Your answer
Follow this Question
Related Questions
Android Unable to track finger touch after orientation change 0 Answers
Android Touch Input GUITexture 2 Answers
GUITexture and Touch Input 1 Answer
Animated Gif 3 Answers
Making a Touch TrailRenderer 1 Answer