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 28, 2013 at 11:16 AM · javascriptraycastzombietower-defense

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 Update ()
 {
     targetTransform = GameObject.FindWithTag("Zombie").transform;
 
     
     
     
     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);
     forward = transform.TransformDirection(Vector3.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 4
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 hamstar · Sep 29, 2013 at 05:32 PM 0
Share

I have just copied/pasted your code and it worked fine for me. I can only imagine you have not set the tag correctly on the Zombie GameObject, or you have multiple objects tagged with "Zombie" and that's caused an issue.

avatar image KeenanPearce · Sep 29, 2013 at 07:43 PM 0
Share

@hamstar This could be the problem, The zombie I have set up is a prefab I made. I was unaware that you couldn't have multiple of the same tag. I will look into this now.

avatar image hamstar · Sep 29, 2013 at 08:31 PM 0
Share

It's not that you can't have multiple objects with same tag, it's just you need to change your logic a bit. An easier way might be to have an invisible sphere collider in a child object of the tower to detect enemies. Set the collider size to the attack range you want, then attach a script that uses OnTriggerEnter or OnTriggerStay to attack a zombie. $$anonymous$$eep transforms of zombies that come in range in a list and add/remove them when they enter/leave the collider.

avatar image KeenanPearce · Sep 29, 2013 at 08:48 PM 0
Share

@hamstar Thank you so much, I didn't even think of that method and I have used it before.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Sep 28, 2013 at 11:18 AM

 function Update ()
 {
 targetTransform = GameObject.FindWithTag("Zombie").transform;

This could potentially find a new target each frame.

Also, in your Raycast

 Physics.Raycast(transform.position, forward, hit, attackRange)

try a more complete expression of 'forward', like Vector3.forward.

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 KeenanPearce · Sep 28, 2013 at 11:52 AM 0
Share

Thanks for the reply, I have put the "targetTransform = GameObject.FindWithTag("Zombie").transform;" into a Start funtion, It seems to be working the same as before.

I also changed the forward expression. but it is still not working.

avatar image
0

Answer by hamstar · Sep 28, 2013 at 01:04 PM

I think there is a mistake when you assign the value to "forward". Vector3.forward is the world forward Vector, not the object's local forward.

Try this:

 var forward : Vector3 = transform.TransformDirection(transform.forward);
Comment
Add comment · Show 3 · 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 KeenanPearce · Sep 28, 2013 at 01:48 PM 0
Share

I just used updated that part, no errors. But that is not the problem i am having, I have debugged the raycast line so it shows in the scene view. It is facing the right way and going the right distance away. but it is not registering when the zombie actually gets touched by the ray.

avatar image hamstar · Sep 28, 2013 at 02:21 PM 0
Share

O$$anonymous$$, I'm not sure that it makes a difference, but you don't need the brackets around zombie: .tag == "Zombie" .

avatar image KeenanPearce · Sep 28, 2013 at 02:39 PM 0
Share

It didn't make a difference but thanks for the suggestion

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

17 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 avatar image avatar image

Related Questions

Disabling a lineRender or Ray when there is no target 1 Answer

Controlled Automatic Fire Rate (RayCasting) 2 Answers

How to make 3D texts show ammo? 4 Answers

GetComponent() problems 2 Answers

Raycasting fail 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