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 digitaldebonaire · Apr 16, 2014 at 03:54 AM · raycastingraycasthitfire

how to setup raycast firing system in this situation

I am working on a tower defense type game. I have a JS script that controls the movement of the tower and the rate of fire that the tower has. It instantiates little projectiles. Those projectiles have their own script attached to their prefab and are launched forward like this:

 function Update () 
     {
          transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
          myDist += Time.deltaTime * mySpeed;
          if(myDist >= myRange)
             Destroy(gameObject);
     }

I have the projectiles colliding with the enemy colliders and then having a message be sent to the enemy collided with. Like this:

 function OnTriggerEnter(other : Collider)
 {
     if(other.gameObject.tag == "Ground Enemy")
     {
         Explode();
         other.gameObject.SendMessage("TakeDamage", myDamageAmount, SendMessageOptions.DontRequireReceiver);
     }



I want to be able to increase the rate of firing speed, potentially infinitely, so I need a new way to do damage calculation. I looked into raycasting but I can't quite grasp how to make it fit into the script. I figure that the raycasting will have to be done in the turret's control script. The control script looks like this:

 function Update () 
     {
         if(myTarget)
         {
             if(Time.time >= nextMoveTime)
             {
                 CalculateAimPosition(myTarget.position);
                 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
             }
             
             if(Time.time >= nextFireTime)
             {
                 FireProjectile();
                 
             }
         }
     }
 
     function OnTriggerEnter(other : Collider)
     {
         if(other.gameObject.tag == "Ground Enemy")
         {
             nextFireTime = Time.time+(reloadTime*.5);
             myTarget = other.gameObject.transform;
         }
     }
 function CalculateAimPosition(targetPos : Vector3)
     {
     var aimPoint = Vector3(targetPos.x-transform.position.x+aimError, targetPos.y-transform.position.y+aimError, targetPos.z-transform.position.z+aimError);
     desiredRotation = Quaternion.LookRotation(aimPoint);
     }

 function FireProjectile()
 {
     audio.Play();
     nextFireTime = Time.time+reloadTime;
     nextMoveTime = Time.time+firePauseTime;    
     CalculateAimError();
     
     for(theMuzzlePos in muzzlePositions)
     {
         Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
         Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
     }
 }


I am not sure where I should put it. To me it makes sense to put is as part of the fire projectile function and have the ray fire forward out from theMuzzlePos.position, theMuzzlePos.rotation just like the instantiated projectile and if it hits the collider with the "enemy" tag then send the "Take Damage" message. I don't know how to do that though and I can't find anything covering this exact use of raycast. Can anyone help me figure out how to set this up?

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

1 Reply

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

Answer by robertbu · Apr 16, 2014 at 04:10 AM

If you want to get rid of the projectile altogether, then raycasting from the muzzle makes sense. You would do something like:

 var hit : RaycastHit;
 if (Physics.Raycast(theMUzzlePos.position, theMuzzlePos.forward, hit) {
     if (hit.collider.tag == tagForSomethingICanDamage) {
        collider.SendMessage("TakeDamage", myDamageAmount, SendMessageOptions.DontRequireReceiver);
     }
 }
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

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

Related Questions

I am using this script to drag/move gameobjects with the mouse. I need the script to ONLY recognize objects with a TAG, like "Painting", How can I fix this? 3 Answers

Is this way of making a raycast correct 2 Answers

Raycast not visible 1 Answer

Raycast detects box collider, but not capsule collider 0 Answers

Working Fine Then Get: No Cameras Rendering after destroying GameObjects 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