Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ryandb2 · Aug 13, 2012 at 01:16 AM · raycasttarget

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 :)

fire.png (17.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

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
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!!!!

:)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sman_47o · Aug 14, 2012 at 05:10 PM 0
Share

$$anonymous$$y pleasure though seth did a much better job explaining. XD

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

RAYCAST Forward error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges