- Home /
This question was
closed Dec 02, 2016 at 09:23 PM by
$$anonymous$$ for the following reason:
The question is answered, right answer was accepted
Question by
$$anonymous$$ · Dec 04, 2016 at 07:42 AM ·
physicsraycastcursorloopsphysics.raycast
Change game-cursor on gameobjects within range
Hi.
I have a problem with the following code:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Move : MonoBehaviour
{
public bool Cursor_enemy = false;
public float radius = 50f;
public Collider[] colliders;
public LayerMask mask;
void LateUpdate() {
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
colliders = Physics.OverlapSphere (transform.position, radius, mask);
foreach (Collider col in colliders) {
if (Physics.Raycast(ray, out hit)) {
if (hit.collider == col) {
Cursor_enemy = true;
} else if (hit.collider != col) {
Cursor_enemy = false;
}
}
}
if (Cursor_enemy == true) {
GameObject.Find ("Canvas").GetComponent<InGameCursor> ().enemy_target = true;
} else {
GameObject.Find ("Canvas").GetComponent<InGameCursor> ().enemy_target = false;
}
}
This code change the game-cursor when it's hovering the first gameobject in the array of colliders found within a radius of 50 units, but it doesn't change the game-cursor when it's hovering any other gameobject that's within 50 units also.
Am I using a wrong method here?
Comment