Unity Enemy AI, health and attack scripts (C#)

Posts 1–11 of 11 · Page 1 of 1
Unity Enemy AI, health and attack scripts (C#)
Thought I would release these, there just simple script and I'm just learning. So please no hate? >.<

Enemy AI

Code:
using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
	public Transform target;
	public int moveSpeed;
	public int rotationSpeed;
	public int maxdistance;
	
	private Transform myTransform;
	
	void Awake(){
		myTransform = transform;
	}


	void Start () {
		GameObject go = GameObject.FindGameObjectWithTag("Player");
		
		target = go.transform;
	
		maxdistance = 2;
	}
	

	void Update () {
		Debug.DrawLine(target.position, myTransform.position, Color.red); 
		

		myTransform****tation = Quaternion.Slerp(myTransform****tation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
		
		if(Vector3.Distance(target.position, myTransform.position) > maxdistance){
		//Move towards target
		myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
	
		}
	}
		
}
Attack

Code:
using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour {
	public GameObject target;
	public float attackTime;
	public float coolDown;
	


	void Start () {
		attackTime = 0;
		coolDown = 2.0f;
	
	}
	

	void Update () {
		if(attackTime > 0)
			attackTime -= Time.deltaTime;
		
		if(attackTime < 0)
			attackTime = 0;
		
		
	if(attackTime == 0) {
		Attack();
		attackTime = coolDown;
		}
	
	}
	
y 	private void Attack() {
		float distance = Vector3.Distance(target.transform.position, transform.position);
		
		
		Vector3 dir = (target.transform.position - transform.position).normalized;
		float direction = Vector3.Dot(dir, transform.forward);
		
				
		if(distance < 2.5f) {
			if(direction > 0) { 
		PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
		eh.AddjustCurrentHealth(-10);
			}
		}
    }
}
Health

Code:
using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
	
	public float healthBarLength;


	void Start () {
	healthBarLength = Screen.width / 2;
	}
	

	void Update () {
	AddjustCurrentHealth(0);
		
	}
	
	
	void OnGUI(){
	GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);	
	}
	
   
	public void AddjustCurrentHealth(int adj) {
	  curHealth += adj;	
		
		if(curHealth < 0)
			curHealth = 0;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
		
		healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
	}
}
EDIT: In AI at the bottom with the *'s replace with (period)rotation. I have no clue why it's censoring.
Very impressive. Nice job. What work have you done in Unity? I'm kinda interested?
Quote Originally Posted by OBrozz View Post
Very impressive. Nice job. What work have you done in Unity? I'm kinda interested?
Not much really, been messing with FPS kit v1.0, and other then that been working on an rpg type game, which these are for. All I have atm is attacking, health, enemy's (with health, AI blah blah). So been working on some stats.

EDIT: Also, think you can give me some links to some weapon models? Doesn't have to be animated. Thanks
Derp 77 views and only one person rplied :3
maybe sometime you could teach me basics
This isn't a really good AI, All its going to do is calculate how far away it is and the rotate towards you and move there. You need to factor in being on a higher ground than the enemy or lower. I might release some paid unity assets soon.
Gratz on learning from tutorials but if your going to release some code make tweak it up at abit.
Like your Health script which is word for word from this tut
2. Unity3d Tutorial - Health Bar 2/2 - YouTube
Make your own function like this

Code:
public float health = 100;

Public void Change (float number)
{
     health += number;
     return number;
}
Now you can change your health by using
example this was in the attack script
Code:
void OnTriggerEnter(Collider other)
{
     Health = health other.gameObject.GetComponnent<Health>();
     Health.Change(-10) * Time.DeltaTime;
}
but good job keep learning

Cheers Jazza (:
Quote Originally Posted by Jzz_ View Post
Gratz on learning from tutorials but if your going to release some code make tweak it up at abit.
Like your Health script which is word for word from this tut
2. Unity3d Tutorial - Health Bar 2/2 - YouTube
Make your own function like this

Code:
public float health = 100;

Public void Change (float number)
{
     health += number;
     return number;
}
Now you can change your health by using
example this was in the attack script
Code:
void OnTriggerEnter(Collider other)
{
     Health = health other.gameObject.GetComponnent<Health>();
     Health.Change(-10) * Time.DeltaTime;
}
but good job keep learning

Cheers Jazza (:
Are you a Unity3D coder by any chance? Or are you a C# coder who understands this?
[COLOR="Silver"]
wish i new a bit of c#
How did you do the Animations of this? Using Blend Tree?
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us