Having some problems trying to implement one feature that doesn’t feel like it should be so difficult: I want a 2D icon to appear at the point a camera is looking at, determined by tag/type of object. For example, if camera looks at floor and uses raycasting to detect a collision, it notices that it has the “floor” tag and thus shows a set of footprints.
I tried using decals to do this, but then realized that probably wasn’t the best way of doing things, so I created a simple quad object and applied an Unlit Transparent texture to it, containing my footprints icon (I have another one with a hand icon for interacting with objects)
Raycasting seems to hit the colliders correctly, but is not placing things (neither decals nor quads) at the actual collision point where the ray would’ve hit. Instead, there are only certain spots where the icon will appear, sort of “stuck” in the middle or edge of a collider/object.
Here’s the code:
void Update () {
RaycastHit objectHit;
Vector3 fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, out objectHit, 10)) {
Transform hitTransform = objectHit.transform;
// When looking around, change the decal icon if a new type of location is viewed; ie. if you can walk, show footsteps, etc.
// Upon decal change, store the new selection as the "current choice" and reset timer.
// If timer reaches 2.5 seconds without changing decal/selection, trigger action based on that choice
if (timer <= 0) {
// DO SOMETHING HERE, LIKE WALK FORWARD TO SPOT OF FOOTSTEPS
// player.GetComponent<CharacterMotor>().MoveTowardsTarget(hitTransform.position.normalized);
timer = 2.5f; // reset timer
} else {
navIcon.transform.position = hitTransform.position;
if (hitTransform.tag == "floor") { // trigger footsteps icon
timer -= Time.deltaTime;
if (currChoice != "floor") { // for each case, if it's not the same icon, switch selection type and reset timer
currChoice = "floor";
timer = 2.5f;
handIcon.SetActive (false);
footIcon.SetActive (true);
}
// Rotate the foot icon in the direction faced
navIcon.transform.rotation = transform.rotation; // for foot icon, feet should face same direction camera is looking
} else if (hitTransform.tag == "use") { // trigger hand icon
timer -= Time.deltaTime;
if (currChoice != "use") {
currChoice = "use";
timer = 2.5f;
footIcon.SetActive (false);
handIcon.SetActive (true);
}
navIcon.transform.rotation = hitTransform.rotation; // for hand icon, rotate it to match the collider face
}
}
}