- Home /
Issue with raycast, hit.collider.tag works sometimes
Desired Result: I want to make it so that when I mouse click in my camera, a turret will spawn. But if a turret is in the way, do not spawn a turret.
The issue: My code appears to work sometimes. Debug log will output that I can't put a turret, other times it will spawn a turret over a turret when it shouldn't.
Code in question:
#pragma strict
private var cam_speed : float = 0.008;
var theTurret : GameObject;
private var isIOSDevice : boolean = false;
var clickDetected : boolean;
var clickDetected2 : boolean;
var clickUnDetected2 : boolean;
var touchPosition : Vector3;
var dragTurretTarget : GameObject = null;
var dragTurret : boolean = false;
function Awake ()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
isIOSDevice = true;
}
else
{
isIOSDevice = false;
}
}
function Update ()
{
//Speed of Cam Scrolling
transform.position.x += cam_speed;
//Detect click and calculate touch position
if (isIOSDevice)
{
//Variables for spawning turret based on a tap
clickDetected = (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began);
touchPosition = Input.GetTouch(0).position;
//Variables for dragging turret with finger
clickDetected2 = (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began);
//touchPosition2 = Input.GetTouch(0).position;
}
else
{
//Variables for spawning turret based on a mouseclick
clickDetected = (Input.GetMouseButtonDown(0));
touchPosition = Input.mousePosition;
//Variables for dragging turret with mousedrag
clickDetected2 = (Input.GetMouseButton(1));
clickUnDetected2 = (Input.GetMouseButtonUp(1));
//touchPosition2 = Input.mousePosition;
}
//Detect click/tap to spawn turret
if (clickDetected)
{
buildTurret();
}
//Detect drag to drag turret
if (clickDetected2)
{
checkDragTurret();
}
if (clickUnDetected2)
{
dragTurret = false;
}
}
function buildTurret()
{
//var mousePos = Input.mousePosition;
var ray : Ray = GameObject.FindWithTag("MainCamera").camera.main.ScreenPointToRay (touchPosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, Mathf.Infinity))
{
if (hit.collider.tag == "TurretCollision")
{
Debug.Log("Can't deploy, another turret is there.");
}
else
{
var newTurret = Instantiate(theTurret, hit.point, Quaternion.identity);
newTurret.transform.position.y = 0.5;
}
}
}
function checkDragTurret()
{
var dragMousePos = Input.mousePosition;
var dragRay : Ray = GameObject.FindWithTag("MainCamera").camera.main.ScreenPointToRay (touchPosition);
var dragHit : RaycastHit;
if (Physics.Raycast (dragRay, dragHit, Mathf.Infinity))
{
if (dragHit.collider.tag == "TurretCollision")
{
dragTurretTarget = dragHit.transform.parent.gameObject;
dragTurret = true;
}
if (dragTurret == true)
{
dragTurretTarget.transform.position = dragHit.point;
dragTurretTarget.transform.position.y = 0.5;
}
}
}
Answer by belvita · Dec 01, 2013 at 09:02 AM
I see that u do not use raycasthit in ur update function this causes unity not to be able to calculate where on each frame ur raycast is hit.for proper raycast detection change the design of ur code then it should work
Also change this
if (Physics.Raycast (dragRay, dragHit, Mathf.Infinity)) To
if (Physics.Raycast (dragRay,out dragHit, Mathf.Infinity))
Your answer
![](https://koobas.hobune.stream/wayback/20220613122922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
get custom class instance of collider hit by raycast? 1 Answer
Physics.Raycast not hitting anything 1 Answer
Detect only UI button click 3 Answers
Raycast to align character on MeshCollider normals 1 Answer
Raycast missing moving Collider 1 Answer