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 KeenanPearce · Sep 29, 2013 at 02:54 PM · javascriptcollisionraycastdestroydetection

Trouble with raycast hitting a tagged object

I am trying to apply damage to an enemy(zombie) when it enters the attack range of a tower. Firstly I have set it up so the raycast works and only goes a certain amount of range, when the zombie enters the range, the tower looks at it. But now I am trying to kill the zombie.

Sorry if this code is sloppy, I haven't been doing this for long. As you can see in the function attack(), I am trying to make the ray detect what it is shooting with a tag, but the Debug.log is not showing up when it is in range.

 var distance;
 var targetTransform : Transform;
 var attackRange = 15.0;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 var fireRate = 0.4;
 var allowedShot : boolean = true;
 var damage = 100;
 var hit : RaycastHit;
 
 function Start()
 {
 
     targetTransform = GameObject.FindWithTag("Zombie").transform;
 
 }
 
 function Update ()
 {
     
 
     
     
     
     distance = Vector3.Distance(targetTransform.position, transform.position);
     
     if (distance < attackRange)
     {
         
         attack();
     }
     
     if (distance > attackRange)
     {
         
     }
     
     Debug.DrawRay(transform.position, transform.forward * attackRange, Color.red);
     
     
     
 }
 
 function attack()
 {
     var rotation = Quaternion.LookRotation(targetTransform.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
     var forward : Vector3 = transform.TransformDirection(transform.forward);
     if (Physics.Raycast(transform.position, forward, hit, attackRange) && allowedShot)
     {
         if (hit.collider.gameObject.tag == "Zombie")
         {
             Debug.Log("zombie hit");
             //hit.transform.gameObject.SendMessage("ApplyDamage", 100, SendMessageOptions.DontRequireReceiver);
             allowedShot = false;
             reload();
         }
     }
 }
 
 function reload()
 {
     
     yield WaitForSeconds(fireRate);
     allowedShot = true;
 
 }


I have got it to work by removing the "if (hit.collider.gameObject.tag == "Zombie"" statement, but that is not what I want as the tower then begins to destroy anything in its path.

Comment
Add comment · Show 1
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 robertbu · Sep 29, 2013 at 02:59 PM 0
Share

You are likely hitting something with your Raycast() you don't expect. Below line 50 insert:

 Debug.Log(hit.collider.name+","+hit.collider.tag);

0 Replies

· Add your reply
  • Sort: 

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

15 People are following this question.

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

Related Questions

2D Collision 1 Answer

collision script to destroy 2 Answers

Raycast object as far as possible? 1 Answer

Move object to raycast point. 3 Answers

Capturing a point 2 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