Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 foxunit · Jun 02, 2011 at 11:06 AM · raycastdirectiondetectfacing

Help with my "CanHitTarget" function

Hello guys,

I just started working with Unity aaaaand right away I need your help ^^".

In theory it should return a boolean if a Target can be hit and that it does. However I do believe, since I dont have much expirience with Raycasts that the ray does not follow the rotation fot he object, this script is attached to, wich is what I want.

To put it even more simple: If the object "faces" the target, "true" shall be returned.

This is my code:

---------------------------------------------------------------------------------------------------------------------------------

function CanHitTarget () : boolean{

     var hit : RaycastHit;
     var direction = transform.TransformDirection(Vector3.forward);
     
 if (Physics.Raycast (transform.position, direction, hit, attackRange))
     return true;
     

 
 else
     return false;

}

Thanks in advance and sorry for my bad english ^^"

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by save · Jun 02, 2011 at 11:35 AM

I think that you don't call CanHitTarget() at the time when it's needed. I wrote you an example of what you could do to make things easier:

 static var targetInSight : boolean = false;
 var target : Transform;
 private var attackRange : int = 100;
 private var lastObject : GameObject;
 function Update () {
 
     var hit : RaycastHit;
     var direction = transform.TransformDirection(Vector3.forward);
 
     if (Physics.Raycast (transform.position, direction, hit, attackRange)) {
         if(hit.collider.gameObject!=lastObject){
             if(hit.collider.gameObject.transform==target){
                 targetInSight=true;
                 print("I'm looking at "+hit.collider.name+" which is my target.");
             } else {
                 targetInSight=false;
                 print("I'm looking at "+hit.collider.name+" and I have no clue what this is.");
             }
         }
         lastObject=hit.collider.gameObject;
     }
 }

The thing is that here it casts a ray on every update, which could get a bit heavy (depending on your other game logic). So you could also put this into a function which only calls if certain actions is being made by your GameObject (such as when it's in a specific area/distance to the player and if it's moving/rotating).

Here's an example of this in a function which only updates on the "watcher's" movement and rotation:

 var target : Transform;
 private var attackRange : int = 100;
 private var lastObject : GameObject;
 private var lastRotation = Quaternion.identity;
 private var lastPosition : Vector3;
 
 function Update () {
     if(transform.position!=lastPosition||transform.rotation!=lastRotation){
         if(checkForTarget()) {
             print("I'm looking at my target.");
         } else {
             print("I'm lost lonely and confused.");
         }
         lastPosition=transform.position;
         lastRotation=transform.rotation;
     }
 }    
 
 function checkForTarget() : boolean {
     var hit : RaycastHit;
     var direction = transform.TransformDirection(Vector3.forward);
 
     if (Physics.Raycast (transform.position, direction, hit, attackRange)) {
         if(hit.collider.gameObject.transform==target) {
             return true;
         } else {
             return false;
         }
     }    
 }

This is just an example as you probably would want to check for player movement and distances also. The first example is easier to implement, the second should later be used for optimizations taken your specific variables into consideration.

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 foxunit · Jun 02, 2011 at 11:47 AM

Thank you very much will try that immediately!

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 save · Jun 02, 2011 at 11:52 AM 0
Share

No worries, remember to use comments on an answer ins$$anonymous$$d of an answer as you did here (it'll be confusing for others). I updated with another example where we turn this into a function ins$$anonymous$$d which only looks for targets on new position and rotation.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I fire a bullet where my player is facing? (C# 2D) 2 Answers

[solved] Raycast get which layer was hit 1 Answer

3th Person Character Facing wrong direction 1 Answer

RayCast to all directions 4 Answers

Change characters direction using camera view 0 Answers


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