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 MrrMaddMann · Nov 02, 2012 at 07:42 PM · c#npc

Need Help Scripting Weapons Using C#

I need to script weapons that can cause damage upon the player, and upon the enemy NPC's. I also need to give the player health, and I still need to entirely script everything about the NPC. I have only recently started learning C# scripting. And currently I have learned about; classes, methods, variables, operations, loops, conditions, arrays and strings. Do I need anything else to script what I need? Also, could someone list what separate things I need to script for the NPC, eg. Engine, Movement. Just all the different systems to make him an NPC capable of killing the player. Thanks. P.S. can no one link me to a full script, 1: because I want to be able to practice my scripting, 2: because this NPC is entirely to any NPC I've seen and so no made script will do what I need this to do. Thank you.

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 harschell · Nov 03, 2012 at 06:13 AM

HI look at following Scripts... i didn't right them though I've taken them from some 3rd person tutorial::

MenuController

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MenuController : MonoBehaviour {
     
     static int difficulty = 1;
     public GUISkin skin;
     private List<Animation> animationComponents;
     private List<AudioSource> audioSourceComponents;
     private float menuOn = 0;
     private float lastTime = 0;
     
     int[] playerHitDamage = new int[5] {  3,  4,  6, 10, 20 };
     int[] playerHeal      = new int[5] {  5,  4,  3,  2,  0 };
     int[] enemyHitDamage  = new int[5] { 10,  5,  2,  2,  2 };
     int[] enemyHeal       = new int[5] {  0,  0,  0,  0,  0 };
     
     // Use this for initialization
     IEnumerator Start () {
         UpdateDifficulty();
         AudioListener.volume = 0;
         yield return 0;
         AudioListener.volume = 1;
     }
     
     // Update is called once per frame
     void Update () {
         // Don't pause in first frame - allow scripts to settle in first
         if (Time.timeSinceLevelLoad == 0)
             return;
         
         float realDeltaTime = (Time.realtimeSinceStartup - lastTime);
         lastTime = Time.realtimeSinceStartup;
         menuOn = Mathf.Clamp01(menuOn + (Time.timeScale == 0 ? 1 : -1) * realDeltaTime * 5);
         
         if (!Screen.lockCursor && Time.timeScale != 0) {
             StartCoroutine(Pause(true));
         }
     }
     
     void OnGUI () {
         if (menuOn == 0)
             return;
         
         GUI.skin = skin;
         
         // PLAY button
         Rect rect = new Rect(0, 0, 150, 75);
         rect.x = (Screen.width  - rect.width ) / 2 + (1 - menuOn) * Screen.width;
         rect.y = (Screen.height - rect.height) / 2;
         if (GUI.Button(rect, "PLAY")) {
             StartCoroutine(Pause(false));
         }
         
         // Difficulty buttons
         rect = new Rect(rect.x - 200, rect.y + 150, rect.width + 400, 40);
         string[] difficulties = new string[] {"NO FAIL", "EASY", "MEDIUM", "HARD", "INSANE"};
         int newDifficulty = GUI.SelectionGrid(rect, difficulty, difficulties, difficulties.Length);
         if (newDifficulty != difficulty) {
             difficulty = newDifficulty;
             UpdateDifficulty();
         }
     }
     
     IEnumerator Pause (bool pause) {
         // Pause/unpause time
         Time.timeScale = (pause ? 0 : 1);
         // Unlock/Lock cursor
         Screen.lockCursor = !pause;
         
         if (pause == true) {
             Object[] objects = FindObjectsOfType(typeof(Animation));
             animationComponents = new List<Animation>();
             foreach (Object obj in objects) {
                 Animation anim = (Animation)obj;
                 if (anim != null && anim.enabled) {
                     animationComponents.Add(anim);
                     anim.enabled = false;
                 }
             }
             objects = FindObjectsOfType(typeof(AudioSource));
             audioSourceComponents = new List<AudioSource>();
             foreach (Object obj in objects) {
                 AudioSource source = (AudioSource)obj;
                 if (source != null && source.enabled /*&& source.isPlaying*/) {
                     audioSourceComponents.Add(source);
                     source.Pause();
                 }
             }
         }
         else {
             // If unpausing, wait one frame before we enable animation component.
             // Procedural adjustments are one frame delayed because first frame
             // after being paused has deltaTime of 0.
             yield return 0;
             foreach (Animation anim in animationComponents)
                 anim.enabled = true;
             foreach (AudioSource source in audioSourceComponents)
                 source.Play();
             animationComponents = null;
         }
     }
     
     void UpdateDifficulty () {
         Object[] objects = FindObjectsOfType(typeof(HealthController));
         foreach (Object obj in objects) {
             HealthController health = (HealthController)obj;
             if (health.gameObject.tag == "Player") {
                 health.healingSpeed = playerHeal[difficulty];
                 health.hitDamage = playerHitDamage[difficulty];
             }
             else {
                 health.healingSpeed = enemyHeal[difficulty];
                 health.hitDamage = enemyHitDamage[difficulty];
             }
         }
     }
 }
 

ProgressBar

 using UnityEngine;
 using System.Collections;
 
 public class ProgressBar : MonoBehaviour {
     
     public HealthController healthController;
     public Texture foregroundTexture;
     public Texture backgroundTexture;
     public Texture2D damageTexture;
     public bool rightSide = false;
     
     void OnGUI () {
         
         // Make rect 10 pixels from side edge and 6 pixels from top. 
         Rect rect = new Rect(10, 6, Screen.width/2-10-40, backgroundTexture.height);
         
         // If this is a right side health bar, flip the rect.
         if (rightSide) {
             rect.x = Screen.width - rect.x;
             rect.width = -rect.width;
         }
         
         // Draw the background texture
         GUI.DrawTexture(rect, backgroundTexture);
         
         float health = healthController.normalizedHealth;
         
         // Multiply width with health before drawing the foreground texture
         rect.width *= health;
         
         // Get color from damage texture
         GUI.color = damageTexture.GetPixelBilinear(health, 0.5f);
         
         // Draw the foreground texture
         GUI.DrawTexture(rect, foregroundTexture);
         
         // Reset GUI color.
         GUI.color = Color.white;
     }
     
 }
 
 
 
 


HealthController

 using UnityEngine;
 using System.Collections;
 
 public class RayAndHit {
     public Ray ray;
     public RaycastHit hit;
     public RayAndHit(Ray ray, RaycastHit hit) {
         this.ray = ray;
         this.hit = hit;
     }
 }
 
 public class HealthController : MonoBehaviour {
     
     public GameObject deathHandler;
     public float maxHealth = 100;
     public float hitDamage = 3;
     public float healingSpeed = 2;
     public GameObject hitParticles;
     public AudioClip hitSound;
     [HideInInspector]
     public float health;
     
     public float normalizedHealth { get { return health / maxHealth; } }
     
     // Use this for initialization
     void OnEnable () {
         health = maxHealth;
     }
     
     // Update is called once per frame
     void Update () {
         if (Time.deltaTime == 0 || Time.timeScale == 0)
             return;
         
         if (health > 0)
             health += Time.deltaTime * healingSpeed;
         health = Mathf.Clamp(health, 0, maxHealth);
     }
     
     void OnHit (RayAndHit rayAndHit) {
         health -= hitDamage;
         health = Mathf.Clamp(health, 0, maxHealth);
         
         if (hitParticles) {
             GameObject particles = Instantiate(
                 hitParticles,
                 rayAndHit.hit.point,
                 Quaternion.LookRotation(-rayAndHit.ray.direction)
             ) as GameObject;
             particles.transform.parent = transform;
         }
         if (hitSound) {
             AudioSource.PlayClipAtPoint(hitSound, rayAndHit.hit.point, 0.6f);
         }
     }
 }

Note: above Scripts are given for educational purpose only... If anyone 've any objection feel free to contact.

Comment
Add comment · Show 2 · 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 MrrMaddMann · Nov 03, 2012 at 08:02 AM 0
Share

Thanks for the scripts. I'll try and adjust them for my needs

avatar image harschell · Nov 03, 2012 at 08:20 AM 0
Share

@$$anonymous$$rr$$anonymous$$add$$anonymous$$ann BTW you can accept the answer & give the ThumpsUp as well :)

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Killable NPC who can kill player 1 Answer

JS to C# Translation? 2 Answers

How to use the animator in children (C#) 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