- Home /
Script to Fire at Objects You Can Hit
I have been banging my head on the wall on what seems like a simple problem. Basically I have the following:
![alt text][1] [1]: /storage/temp/2685-fire.png
The cannon is run by an AI routine that determine the closest target and where the target will be. The problem is I don't want it to target any targets that are on the other side of the planet, basically if it can't hit it, don't fire and choose a different target.
I have tried two approaches to this:
Raycast from Cannon to Target - No matter what I try I can't get this to work. Basically I try to raycast from the cannon to the target, I have a static sphere collider on the black planet in the middle and a sphere collider/rigidbody on the projectile the cannon creates. On creation I raycast to the target and then check to see if the hitinfo is the planet. But I never get a return on the Raycast other than the hit on the target. Here is the code I am trying (ap is the target, we are in the projectile, so transform.position is the local projectile):
foreach(asteroidProjectile ap in myCameraScript.asteroids)
{
if(Vector3.Distance(transform.position,ap.transform.position)<distance)
{
RaycastHit hitInfo;
if(Physics.Raycast(new Ray(transform.position,ap.transform.position.normalized),out hitInfo,Mathf.Infinity))
{
if(hitInfo.collider.gameObject.tag!="Ground")
{
target = ap.gameObject;
distance=Vector3.Distance(transform.position,ap.transform.position);
}
}
}
}
No matter what I do the raycast doesn't ever hit the planet tagged "Ground". I know collisions are working as when the projectile is fired it does impact with the planet. I have tried using the local collider for the projectile as well as spherecasting and RaycastAll looping through the hitInfo. All seem to produce the same results.
Measure the angles between the two points - Only fire if the angle is greater than a certain degree. I figured this would be computationally cheaper than raycasting but I can't seem to figure out if this is possible as my Vector math is failing me:
float angle = Vector3.Angle(transform.position,ap.transform.position);
It doesn't seem to make any sense to me what this is returning, basically if I try to tell this to only to target if the angle is a small number like <= 15 degrees and >= 0 degrees, it still seems to shoot at anything on the screen. There are certain objects it won't target but I think it is because this is not the angle I am really looking for.
Any thoughts or tips would be greatly appreciated as I have been stuck on this for a week :)
Thanks!
Edit - Worked like a charm, Seth's update that took into account position vs direction was correct. My code still didn't work because I was Raycasting into my boundaries and causing a few issues there, but now everything is working great!
Thanks you so much for the help, you saved my weeks worth of work and my sanity!
Sorry for doing this as an edit but I can't seem to post a response for some reason :)
Answer by Seth-Bergman · Aug 13, 2012 at 01:36 AM
if(Physics.Raycast(new Ray(transform.position,ap.transform.position.normalized),out hitInfo,Mathf.Infinity))
this is incorrect, to declare a Ray we use: Ray(origin,direction)
you are using a position rather than a direction..
try:
if(Physics.Raycast(transform.position,(ap.transform.position - transform.position) ,out hitInfo,Mathf.Infinity))
that's what you meant to do..
also, you should use Debug.DrawRay to see where your ray is actually going, this is very helpful in figuring this out (in the Update):
Debug.DrawRay(transform.position,(ap.transform.position - transform.position) , Color.green);
Answer by sman_47o · Aug 13, 2012 at 02:11 AM
(sorry i realized someone had just beat me to it when I submitted my answer) What I would do is this:
Cast a ray when youre object becomes active, but only shoot the projectile if hitInfo.distance is less than your max allowed distance. And to make the ray cast align with the target (if I'm doing this right in my head) you would get the position of the hit and subtract the position of the canon and then normalize that vector to get the direction of the target.
So sometin like this :
Function Awake(){ //this is off the top of my head so sorry for typos
var range : float; //your max range
var direction : Vector3 = closesttarget.position-tranform.position;
var speed : float; //mess around with this one til you're happy
If(physics.Raycast(transform.position , direction, hit info, range)){
//then set the velocity of the projectile here to direction*speed
}
}
Answer by ryandb2 · Aug 13, 2012 at 05:20 AM
Awesome posts! That did it. I was being dumb about the direction of the ray, but there was something else that this lead me to. I was checking to ensure the Ray didn't collide with the ground, but I also have boundary objects and I was still allowing those to be targets. After I fixed the code by utilizing Seth's code it still wasn't working because of this.
Thank you so much, you both saved my weekend from being a total waste on this project!!!!
:)
$$anonymous$$y pleasure though seth did a much better job explaining. XD
Answer by ryandb2 · Aug 13, 2012 at 04:37 AM
Thanks guys! Seth your comment was spot on, I was being dumb about the direction vs position issue. But when I implemented your change it still didn't work. I then fount that my tracking logic was no excluding my boundaries as possible targets so that is where the random results were coming from.
Thank you so much for saving my weeks worth of work and my sanity!
Your answer
Follow this Question
Related Questions
How to make an object to look at Raycast Hit Point? 1 Answer
Help With RayCasting 1 Answer
UI element in 3D space, reacts to click despite raycast target being infront 0 Answers
Look at enemy 1 Answer