well basicly… my question is about raycasting… yhea i know i said i’ll never use it again… but it solve my falling problem so well! that i realized this is the best way to detect things on the enviroment
that been said…
im doing a homming misile in my game… and the idea i have for this misile is to follow the nearest target from the origin point… now… i know how to work with tags and these stuffs… i tagged all the enemies with a label called “aimable” … what i need now is to cast a sphere that goes growing bigger and bigger until it detects an object with the tag aimable on it… so the misile can go in that direction
is it possible to cast a sphere?.. i tried reading the RaycastAll section… but it seems it only cast foward or in other straight direction ( like -z, -y etc… ) i want to check the whole ambient instead of just casting a straight line in one sole direcition
Is is possible (see SphereCast) but could be very expensive for the CPU. If you just want to find the closest enemy there are better ways to do it, like looping through all the enemies and calculating their distance with Vector3 math
You could also use a sphere collider with is trigger set to true, and use script to do something like this (start with a very small sphere collider radius on the prefab):
var mySphere : SphereCollider;
var targets = new Array();
private var wasTriggered : boolean;
function Start(){
mySphere = GetComponent(SphereCollider);
}
function Update(){
if(wasTriggered == false)
mySphere.radius += 0.1; //tweak to taste
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "aimable" wasTriggered == false){
targets.Add(other.gameObject);
wasTriggered = true;
}
}
well… it may even be better but,… i have no idea how to work with layers… im more confortable doing my tag thing… you know… at least while im noobing arround the software xD
but thanks for the tip anyway!..
as for the spherecast… YES! its eaclty what i was looking for… too bad i dont undestand that example…
is said something about // Cast character controller shape 10 meters forward, to see if it is about to hit anything
10 meters foward?..but its a sphere colider… i want it to check foward, backward up down , diagonal… everywhere at the same time -.-
can you guys give me an example of coding ?
just until the park that checks if collided with a tag
EDIT>> iooops sorry when i started replying i didnt see legend411 reply!.. thanks for the example! i’ll consider using a colider instead of raycasting!..
wait… legend411… you mean… i gotta attach a small sphere collider on the character, an then raise up its radius via scripting?.. or i have to attach the sphere collider into the misile itself?
and… since my character already has a colider, another colider would… colide on it?.. i mean… it can bug some things like jumping or dashing… if he colides on himself
Collider[] targets = Physics.OverlapSphere(transform.position, radius_of_sphere);targets will be an array with all your objects so you can do stuff to them…
for(collider : Collider in targets)
{
print(collider.name);
}
The documentation makes it sound like the function will return colliders if they are actually inside the sphere not being intersected by it but I am probably reading it wrong.
Warp, if you want to detect the nearest “aimable” enemy to the player, then yes, you would attach it to the character. you’d probably have to put it on an empty gameobject parented to the player though, because as you said, your player object probably already has a collider/character controller.
When you set a collider to isTrigger = true, it no longer does any collision physics. You wouldn’t have to worry about the player object getting added to the targets array either, because right inside OnTriggerEnter() you’re doing the tag check. If you REALLY wanted to be safe, inside Start() you could do Physics.IgnoreCollision(PlayerObject); (assuming you cache a reference to your player object).
okay… lets just confirm if now i get everthing you guys are saying
i have to add this script
var mySphere : SphereCollider;
var targets = new Array();
private var wasTriggered : boolean;
function Start(){
mySphere = GetComponent(SphereCollider);
}
function Update(){
if(wasTriggered == false)
mySphere.radius += 0.1; //tweak to taste
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "aimable" wasTriggered == false){
targets.Add(other.gameObject);
wasTriggered = true;
}
}
to the player, along with a sphere collider with is trigger = true… and then drag the sphere coliider into the “my sphere” transform variable
so far so good
as i undestand, this scripts makes the sphere keep growing and growing until it finds something with the tag aimable, then the sphere turns the is triger to false so it can colide, and at this particular moment, i should have a script ready on the target, so when he colides with this sphere he will start calling for the misile to go on his direction… is that it?
edit >> nevermind, i got it… all this is to turn on that var Wastriggered… when the var is turned to true, the sphere stops growing because… it already found its target right?.. well… but it seems that i still need raycasting because …, ok i have a script that knows how to detect the nearest aimable tag… but how can i know what that was that?.. they’re all have the same name… “aimable” i know that i hit one of then but i dont know whitch one… so i still can shoot a projectile in that area
NOW I GOT IT! thanks man!.., this scripts works flawless! even when there are two very close targets!!!
this is great
so, when the sphere hits the first object with the tag aimable… it will add this target in the transform variable “targets” and then… all i have to do is instantiate the misile on the “shootpoint” and transform.position to “targets”