- Home /
Raycast on touch
var hit: RaycastHit;
var manager : GameObject;
function FixedUpdate()
{
if (Input.touchCount > 0)
{
var theTouch : Touch = Input.GetTouch(0);
var ray = Camera.main.ScreenPointToRay(theTouch.position);
if(Physics.Raycast(ray,hit,100))
{
if(hit.transform.gameObject.tag == "planet")
{
if(Input.touchCount == 1)
{
if(theTouch.phase == TouchPhase.Began)
{
var script : PlanetActionMenu;
script = manager.GetComponent(PlanetActionMenu) as PlanetActionMenu;
script.PlanetSelected();
script.planet = hit.transform.gameObject;
hit.transform.Translate(Vector3(1,0,0));
}
}
}
if(hit.collider.gameObject.name == "CameraMoveCollider")
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Moved)
{
transform.Translate(theTouch.deltaPosition.x/-6,theTouch.deltaPosition.y/-6,0);
if(transform.position.x < 5)
{
transform.position.x = 5;
}
if(transform.position.x > 485)
{
transform.position.x = 485;
}
if(transform.position.y < 5 )
{
transform.position.y = 5;
}
if(transform.position.y > 485)
{
transform.position.y = 485;
}
}
}
}
}
}
}
for some reason this never works:
if(hit.transform.gameObject.tag == "planet")
{
if(Input.touchCount == 1)
{
if(theTouch.phase == TouchPhase.Began)
{
var script : PlanetActionMenu;
script = manager.GetComponent(PlanetActionMenu) as PlanetActionMenu;
script.PlanetSelected();
script.planet = hit.transform.gameObject;
hit.transform.Translate(Vector3(1,0,0));
}
}
}
i tried hit.collider.tag, hit.collider.gameObject.tag and hit.transform.tag. Nothing works, the 2nd if works just fine
Answer by Ashish Dwivedi · Feb 10, 2014 at 04:10 AM
Are you checking on editor? If you are checking on editor then this code would not work because on editor "Input.touchCount" will always be 0 and your first condition is "Input.touchCount > 0" that will remain false always on editor. So you have to write code so that work for both means on editor as well as on device.
Answer by xandermacleod · Feb 08, 2014 at 12:44 AM
try including the word 'out' before the word hit when firing your raycast. on line 10
Answer by rejwan1 · Feb 08, 2014 at 12:50 AM
If you encounter such a problem, always use Debug.Log to try and figure out what's wrong & use MonoDevelop to debug break and go step by step. This way you can see what the raycasting actually hit (if it hit anything at all)
You might have forgotten to tag your object correctly, give it a boxcollider or anything of the sort.
Assuming it hit your object and we got into the 2nd "if" - the fact that you're doing this in FixedUpdate might be the source of the problem. A general rule of thumb is that you should only do physics related things in FixedUpdate. Input should be handled in Update.
Another general rule of thumb is that if you want to get touch/mouse input, you should raycast at Mathf.Infinity - that way you will always "hit" something, even if it's far away
I'm using a spehre collider, could that be the issue? everything else should be fine, i checked for every possible error i could have done(distance for raycast is ok, tag is correct etc...) Btw thanks for replying
it should defo be collider.gameObject.tag so go that way for a start.
camera tag not set to mainCamera perhaps? theres very little here i can spot that should make it wrong with the script. more with your scene.
after that id be grasping at straws like your Planet tag has a capital ins$$anonymous$$d of all lower case like in this script etc.
Your answer
Follow this Question
Related Questions
Exclude tag from raycast 1 Answer
Make tagged objects explode with ray cast? 1 Answer
This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 1 Answer
Rays and tags help? 1 Answer
gameObject tag 2 Answers