Results 1 to 11 of 11
  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)

  3. #2
    OBrozz's Avatar
    Join Date
    May 2011
    Gender
    male
    Posts
    819
    Reputation
    65
    Thanks
    813
    Very impressive. Nice job. What work have you done in Unity? I'm kinda interested?

  4. #3
    pspdude's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    20
    Reputation
    10
    Thanks
    2
    My Mood
    Amazed
    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
    Last edited by pspdude; 02-20-2012 at 12:00 AM.

  5. #4
    pspdude's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    20
    Reputation
    10
    Thanks
    2
    My Mood
    Amazed
    Derp 77 views and only one person rplied :3

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

    OBrozz (03-03-2012)

  7. #5
    parag0nx9's Avatar
    Join Date
    Jun 2011
    Gender
    male
    Location
    riding kangaroos
    Posts
    313
    Reputation
    -33
    Thanks
    14
    My Mood
    Sad
    maybe sometime you could teach me basics


    Feel free to add me on steam: Dvorak

  8. #6
    Red_RA1N's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Posts
    9
    Reputation
    10
    Thanks
    0
    My Mood
    Breezy
    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.

  9. #7
    Jàzzà_'s Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    185
    Reputation
    11
    Thanks
    97
    My Mood
    Cool
    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 (:
    Last edited by Jàzzà_; 11-05-2012 at 09:36 PM. Reason: Changed 10 to minus 10 to take health away


    Applications:
    Jàzzà InjectXD | Radio♫ v1.1 | File Name Sorter
    Better than Bombsaway707 Radio (:

  10. The Following 2 Users Say Thank You to Jàzzà_ For This Useful Post:

    OBrozz (04-15-2013),OOZEStudios (11-18-2013)

  11. #8
    ResistanceRage's Avatar
    Join Date
    Apr 2012
    Gender
    male
    Location
    Sweden, Malmö
    Posts
    151
    Reputation
    10
    Thanks
    47
    My Mood
    Happy
    Good job very useful!
    My signature sucks...

  12. #9
    Arrxzon's Avatar
    Join Date
    Jul 2012
    Gender
    male
    Location
    Ireland
    Posts
    447
    Reputation
    10
    Thanks
    28
    My Mood
    Psychedelic
    wish i new a bit of c#

  13. #10
    OBrozz's Avatar
    Join Date
    May 2011
    Gender
    male
    Posts
    819
    Reputation
    65
    Thanks
    813
    Quote Originally Posted by Jàzzà_ 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"]

  14. #11
    Gulzxc's Avatar
    Join Date
    Aug 2016
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0
    How did you do the Animations of this? Using Blend Tree?

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