• 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
2
Question by BKOrtiiz · Jul 02, 2012 at 10:55 PM · raycastmobileshootingautomatichitbox

Raycast damage script

I am doing an IPhone shooter however i don't want to have a shoot button is there any way or thechnique (script) you know to shoot a raycast continuously out of the players gun so that when the ray makes contact with a box around the enemy it starts shooting and the bullets in the magazine go down?

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
3

Answer by aldonaletto · Jul 02, 2012 at 11:31 PM

Doing this in Update may cause excessive impact in the performance, but maybe a more spaced raycasting could solve the problem - something like this (weapon script):

var bullets: int = 100; // ammo count var shotSound: AudioClip; // drag the shot sound here var shotInterval: float = 0.25; // interval between shots var shotRange: float = 100; // range of the shot var shootEnabled: boolean = true; // allows disabling the shots

function Start(){ var trf = transform; // a little optimization while (true){ // loop forever if (shootEnabled && bullets > 0){ // if there are bullets and shooting enabled... var hit: RaycastHit; // do a raycast if (Physics.Raycast(trf.position, trf.forward, hit, shotRange)){ // if the enemy trigger childed to the enemy is hit... if (hit.transform.gameObject.CompareTag("EnemyTrigger")){ audio.PlayOneShot(shotSound); // play the shot sound... hit.transform.SendMessageUpwards("ApplyDamage", 5); // apply damage... bullets--; // and count the shot } } } yield WaitForSeconds(shotInterval); // always wait shotInterval } } You must have a function ApplyDamage(damage: float) in the enemy script, and the "box" you've mentioned must be a trigger volume childed to the enemy.

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
1

Answer by diddykonga · Jul 02, 2012 at 11:28 PM

 using UnityEngine;
 using System.Collections;

 public class AutoShoot : MonoBehaviour
 {
  public float fireRate; // How quick to shoot
  public float damage; // Damage to be applied
  public float shootDistance; // How far to check for a hit
 
  private bool canShoot;
 
  void FixedUpdate ()
  {
  RaycastHit info;
  if(Physics.Raycast(new Ray(transform.position, transform.forward), out info, shootDistance))
  {
  if(canShoot)
  {
  // Do damage stuff here
 
  if(fireRate > 0)
  {
  canShoot = false;
  Invoke("ShootDelay", fireRate); // Delay shooting
  }
  }
  }
  }
 
  private void ShootDelay() 
  {
  canShoot = true; 
  }
 }

Here you go, i think this should work, i havent tested it though :)

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 sStarS · May 16 at 07:09 PM 0
Share

i dont get it , i tried it but doesnt work..

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

7 People are following this question.

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

Related Questions

Third Person Shooting Script Stopped Working 1 Answer

FPS shooting 1 Answer

Character keeps snapping between two finger positions [Mobile, Android, Raycast] 0 Answers

problem with scripting 1 Answer

Arrow not shooting the in right orientation 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