Results 1 to 11 of 11

Threaded View

  1. #1
    pspdude's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    20
    Reputation
    10
    Thanks
    2
    My Mood
    Amazed

    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.
    Last edited by pspdude; 02-18-2012 at 06:35 PM.

  2. The Following User Says Thank You to pspdude For This Useful Post:

    FlappaDoodles (07-10-2012)

Similar Threads

  1. RSBot - Running and adding Scripts
    By Jigsaw in forum Runescape Tutorials
    Replies: 33
    Last Post: 09-03-2011, 08:01 AM
  2. How to compile and add scripts directly into RSBot.jar!
    By Death751 in forum Runescape Tutorials
    Replies: 3
    Last Post: 05-10-2011, 08:56 PM
  3. [Release] What happens if you combine AC130 and Attack Heli..
    By Arasonic in forum Call of Duty Modern Warfare 2 Server / GSC Modding
    Replies: 13
    Last Post: 11-22-2010, 12:48 PM
  4. Health and ammo!
    By H4x4ever in forum WolfTeam General
    Replies: 10
    Last Post: 10-08-2008, 08:40 PM
  5. Inf Health And Look!!!!
    By Blondenut1 in forum WarRock - International Hacks
    Replies: 10
    Last Post: 06-02-2007, 09:43 AM