• 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 seb-lopez · Apr 29, 2015 at 03:47 PM · aishootingraycastingsmooth

raycast cooler

I made a Ai shoot script that works with raycasting but the problem is that the raycast goes non stop and kills me in a second, and i'm trying to find a way to smoth it or at least 1 shot of raycast per second. here is my ai shoot script(feel free to copy if you want i don't mind).

  var player : Transform;
 var safeDist : float = 15;
 var currentDist : float;
 var shooting : boolean = false;
 
 var canShoot: boolean = false;
  var BloodSplat : GameObject;
   var sparkEffect : GameObject;
 
 var gunTip : Transform;
 
 var range : float = 1000;
 var force: float = 1000;
 var shotDelay : float = 0.5;
 
 var Damage : int = 1;
 var Bullet : GameObject;
 
 
  var ShootTimer : float = 1;
  var ShootCooler : float = 10;
 
 function Update () {
 
 
  
     currentDist = Vector3.Distance(transform.position, player.position);
  
     if(currentDist < safeDist){
     //transform.LookAt(player);
     if(!shooting){
     
       shootStuff();
     
      
       Instantiate(Bullet, gunTip.transform.position, gunTip.transform.rotation);//this just for decoration
     }
  
 }
 }
 
 
 
 
  
 function shootStuff(){ // the part that shoot
  
       var hit : RaycastHit;
 
         var DirectionRay = transform.TransformDirection(Vector3.forward);
 
         Debug.DrawRay(transform.position , DirectionRay * range , Color.yellow);
 
      if(Physics.Raycast(transform.position , DirectionRay , hit , range)){
      var particleClone1 = Instantiate( sparkEffect , hit.point, Quaternion.LookRotation(hit.normal));
          Destroy(particleClone1.gameObject, 0.2);
                       
       if ( hit.rigidbody){
           
           hit.rigidbody.AddForceAtPosition( DirectionRay * force , hit.point);
          
           hit.collider.SendMessageUpwards ("HeroeHit" , Damage , SendMessageOptions.DontRequireReceiver);
          if( hit.transform.tag == "Player" ) {
           var particleClone = Instantiate( BloodSplat, hit.point, Quaternion.LookRotation(hit.normal));
          Destroy(particleClone.gameObject, 0.2);
          
         }
         }
         }
         }
         
  
 

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 Soos621 · Apr 29, 2015 at 04:03 PM

You can either take the bullet when you instantiate it and add a force in the direction your ray at is shooting, then on the bullet put a script that tells the object you are shooting to destroy itself

Or

You can use a Coroutine to raycast out then yield return new waitforseconds to have it wait to "wait" to destroy the object

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 seb-lopez · Apr 30, 2015 at 08:29 PM

how can i coroutine my raycast? and is it possible to make it burst with this systeme ?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Problem with raycasting in direction of mouse 0 Answers

Why does my AI miss me every few feet? 1 Answer

is there a good ai car tutorial about 2 Answers

How do I make my raycast shooting script full auto 1 Answer

How to stop enemies from shooting each other 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