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 BlackWingsCorp · Feb 27, 2013 at 07:43 PM · javascriptfpsraycasthit

Shooting system not working

Hey guys how's it going? I am trying to make a script for shooting using RayCast I wrote it so that when the RayCast hits an object tagged "enemy" and the player presses fire1 enemy will take damage but no damage is done:

 var bullet : Rigidbody;
 var speed : float = 20;
 
 function Start () {
 attackTimer = 0;
 }
 
 function Update () {
 var hit : RaycastHit;
 if(Physics.Raycast(transform.position, transform.forward, hit, 3)){
 if(hit.collider.gameObject.tag == "Enemy"){
 if(Input.GetButtonUp("Fire1")){
 Shoot();
 }
 }
 }
 }
 
 
 private function Shoot (){
 var newBullet : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
 newBullet.velocity = transform.forward * speed;
 var eh : EnemyHealth = GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }

Any ideas/fps tutorials?

Comment
Add comment · Show 2
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 · Feb 27, 2013 at 08:13 PM 0
Share

I'm not sure what you want to do here. GetButtonUp() will only return true for a single frame when the button is lifted. GetButtonDown() might be what you are looking for. Put a debug statement inside the raycast to make sure the raycast is working and hitting the enemy (ins$$anonymous$$d of some other collider).

 Debug.Log("Name = "+hit.collider.name); 
avatar image BlackWingsCorp · Feb 27, 2013 at 08:27 PM 0
Share

the script is attach to the pistol which is attached to the player I'm trying to cast a ray from the gun, checks if the ray hits the enemy (hit.gameObject.tag == "Enemy) then if the player hits fire1 the enemy's health drops (eh.AdjustHealth (-5)) I put a Debug.Log function but it didn't work and the console doesn't show errors. Do you think I have a better chance trying the OnCollisionEnter(col : Collision) and place it on the bullet prefab?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by BlackWingsCorp · Feb 27, 2013 at 11:35 PM

Well the ray is casting but only 1 enemy is taking damage here's the script:

 var force : float = 10;
 var range : float = 200;
 var enemies : GameObject[];
 var enemy : GameObject;
 function Start () {
 enemies = GameObject.FindGameObjectsWithTag("Enemy");
 enemy = enemies[0];
 }
 
 function Update () {
 RayShoot();
 }
 
 
 function RayShoot (){
 var hit : RaycastHit;
 var directionRay = transform.TransformDirection(Vector3.forward);
 Debug.DrawRay(transform.position, directionRay * range, Color.blue);
 if(Physics.Raycast(transform.position, directionRay, hit, range)){
 if(hit.collider.tag == "Enemy" && Input.GetButtonUp("Fire1")){
 enemy = enemies
 var eh : EnemyHealth = enemy.GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }
 }
 }

I want enemy to change depending on where the player is aiming. Any idea guys?

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

9 People are following this question.

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

Related Questions

Enemy death help 1 Answer

Distance on fps 3 Answers

Lens-Flare Muzzleflash 0 Answers

gun scope cam not working 3 Answers

Problem With Weapon Firing Mechanism 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