Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bawbieboo · Mar 05, 2016 at 06:30 PM · shootingrobotgunfire

Trying to make a gun, but it wont work? Please help.

I have been working on a game for some time now and I'm trying to make a FPS. My problem is that it doesn't do damage to the robot? Please help. Ill send the RobotScript, and the Shooting Script.


Shooting Script

using UnityEngine; using System.Collections; using UnityEngine.Networking;

public class Shooting : NetworkBehaviour {

 public RobotScript RobotScript;

 public int minWeoponDamage = 25;
 public int maxWeoponDamage = 50;

 public int weoponRange = 100;

 public bool debugMode = true;

 public Camera FPSCamera;

 private void Update()
 {
     Ray ray = FPSCamera.ScreenPointToRay (new Vector2 (Screen.width / 2, Screen.height / 2));
     RaycastHit hitInfo;

     if (debugMode == true)
     {
         Debug.DrawRay(ray.origin, ray.direction * weoponRange, Color.green);
     }

         if(Input.GetKeyDown(KeyCode.Mouse0))
     {
         if(Physics.Raycast(ray, out hitInfo, weoponRange))
         {
             if(hitInfo.collider.tag == "Enemy"){
                 Debug.Log ("Enemy Hit");
                 RobotScript.instance.Health = 10;
             }
         }
     }
 }

}


Robot Script

using UnityEngine; using System.Collections;

public class RobotScript : MonoBehaviour {

 public static RobotScript instance;
 public int Health = 100;
 
 static int MaxHealth = 100;
 public static int DeathHealth = 1;
 


 void Start () {
     instance = this;
 
 }
 

 void Update () {
     if (Health < DeathHealth) {
         Destroy (this);
         Debug.Log("Enemy should be dead");
     }
 }

}


Extra Info

The Robot Script is attached to a Game Object.

The Robot is NOT a player.

The Robot is a enemy.

The Robot has a tag called "Enemy", on it.

Thanks for taking your time reading, and for helping!

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
1
Best Answer

Answer by Glurth · Mar 05, 2016 at 06:37 PM

It looks like, if the robot is hit you call this line:

 RobotScript.instance.Health = 10;

which will set it's health to 10. I suspect you meant to use -= rather than =, so that it will SUBTRACT 10 from the health. e.g.

 RobotScript.instance.Health -= 10;

this is equivalent to:

  RobotScript.instance.Health = RobotScript.instance.Health - 10;

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 Bawbieboo · Mar 05, 2016 at 06:45 PM 0
Share

Im trying this, and thankyou for responding!

avatar image Bawbieboo Bawbieboo · Mar 05, 2016 at 06:47 PM 0
Share

It is working, but it doesn't actually destroy the object? Like in console it says

Enemy Hit (10) Enemy should be dead (1)

So i hit it 10 times and it said that, but the damage isn't actually taking?

avatar image Bawbieboo · Mar 05, 2016 at 06:54 PM 0
Share

I got it working thanks Glurth!

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

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

Related Questions

How to make gun shoot in the crosshair & automatic fire (Javascript) 1 Answer

How to make an automatic fire mode for a gun (Javascript) 0 Answers

How do I make my ships lasers come out straight 0 Answers

Shooting Gun Script 4 Answers

Reload only when not firing 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