- Home /
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.
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.
Thanks for the scripts. I'll try and adjust them for my needs
@$$anonymous$$rr$$anonymous$$add$$anonymous$$ann BTW you can accept the answer & give the ThumpsUp as well :)
Your answer
Follow this Question
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