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 Str0phicus · May 05, 2014 at 10:08 AM · enemyfollow playerhealth

Enemy attack/follow Player script?

Trying to write a script to get a skeleton (an enemy) to follow my character and attack when close enough by punching/swiping. I have tried adding colliders to the skeleton's arms and causing them to take health away from the player, but the main issue is to get the skeletons to move with the correct animations triggering at the right times. At the moment I also have a collider on the player of course, and a collider around the player's sword. Whenever the collider of the player or sword come into contact, however, the skeleton slides away as if on an icy surface... Did I do something wrong in the script for the enemy?

This is what I have at current (Errors about the 'get_time' are also showing in the console window but I can't fix them).

#pragma strict

var target : Transform; var moveSpeed = 2.5; var rotationSpeed = 4; var attackThreshold = 3; var chaseThreshold = 15; var giveUpThreshold = 25; var attackRepeatTime = 1;

private var chasing = false; private var attackTime = Time.time;

var myTransform : Transform;

function Awake() { myTransform = transform; }

function Start() { target = GameObject.FindWithTag("Player").transform; }

function Update () {

 var distance = (target.position - myTransform.position).magnitude;
 
 if (chasing) {
 
     
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
     
     if (distance > giveUpThreshold) {
         chasing = false;
     }
 
     
     if (distance < attackThreshold && Time.time > attackTime) {
         animation.Play("attack");
         attackTime = Time.time + attackRepeatTime;
     }
 
 } else {
     
 
     
     if (distance < chaseThreshold) {
         chasing = true;
         animation.Play("run");
     }
 }

}

Comment
Add comment · Show 3
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 andyspeak · May 05, 2014 at 11:50 AM 0
Share

https://www.youtube.com/watch?v=YYqzz1dy3Ak

link to a tutorial series watched this last year best thing i ever did i watched all 300 but the 1ST 5 is what your looking for its c#

avatar image Str0phicus · May 06, 2014 at 03:46 AM 0
Share

Ahh ok. Will look at that, I prefer JS tho

avatar image AlucardJay · May 06, 2014 at 04:13 AM 0
Share

I did a video series last year in uJS. Probably start at video 23

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Harryliu · May 06, 2014 at 04:17 AM

I have a simple enemy AI. see if this helps. so it would stop moving if it is far away from your itlink text would be harless, and if you get closer it would start chasing you.


enemyai.txt (7.9 kB)
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 Str0phicus · May 06, 2014 at 04:41 AM 0
Share

Wow, ok. This looks like overkill for my skeleton, but you think it will work effectively? I'll try it then (ins$$anonymous$$d of my above script). I'll tell you how it goes in a few $$anonymous$$utes.

avatar image Str0phicus · May 06, 2014 at 04:45 AM 0
Share

Okay, using that script bombards me with error messages.

avatar image
0

Answer by guillopuyol · May 06, 2014 at 04:17 AM

The sliding effect you mention might be the result of the colliders not being set up as triggers, so the player is in fact pushing the skeleton away. Also, I'm not sure about the setup of your game, but having colliders on the sword seems like overkill. How about setting it so that if the skeleton is looking towards the player +- a few degrees, and within range, then play the attack animation and deduct health accordingly.

Comment
Add comment · 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
0

Answer by $$anonymous$$ · Jun 30, 2018 at 04:01 PM

Hello , ( you have post that question in 2014 and i see him in 2018 Im not sure if you got the solution or not , so i deside to answer , maybe it will help the others )

I have tried your script and i see some errors messages , if your problem is the Errors messages , so i find a solution . Just add new one of this mark } in the last of the script , to close the Update function and it will fixed like it happend to me .

I hop it helped .

Comment
Add comment · 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
0

Answer by H3yPlaya · Jun 30, 2018 at 04:15 PM

I have my own game kinda like this, and I don't use lookRotation, it's inefficient. instead I use setDestination and a navmesh, like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class PlayerAttack : MonoBehaviour {
     
     public GameObject[] Enemys;
     public GameObject EnemyAttacking;
     public LayerMask groundLayer;
     public NavMeshAgent playerAgent;
 
     private float AttackTimer = 0.0f;
 
     public int AttackDamage = 30;
     public float AttackSpeed = 0.5f;
     public bool Melee = false;
     public float AttackRange = 5.0f;
 
     void Start()
     {
         if (Enemys == null)
         {
             Enemys = GameObject.FindGameObjectsWithTag("Enemy");
         }
 
         if (Melee)
         {
             AttackRange = 0;
         }
     }
 
     void Update () {
         this is a simple left click on an AI to attack it.
         if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 EnemyAttacking = hit.transform.gameObject;
             }
         }
 
         if (Input.GetMouseButtonDown(1))
         {
             EnemyAttacking = null;
             AttackTimer = 0;
         }
 
         if (EnemyAttacking != null)
         {
             float AIDistance = Vector3.Distance(gameObject.transform.position, EnemyAttacking.transform.position);
 
             if (Melee)
             {
                 if (AIDistance > 1)
                 {
                     Chase();
                     AttackTimer = 0;
                     playerAgent.stoppingDistance = 0;
                 }
                 else
                 {
                     AttackTimer += 1 * Time.deltaTime;
 
                     if (AttackTimer >= AttackSpeed)
                     {
                         Attack();
                         AttackTimer = 0;
                         playerAgent.stoppingDistance = 1;
                     }
                 }
             }
             else
             {
                 if (AIDistance > AttackRange)
                 {
                     Chase();
                     AttackTimer = 0;
                 }
                 else
                 {
                     playerAgent.transform.LookAt(EnemyAttacking.transform.position);
                     playerAgent.stoppingDistance = AttackRange;
                     this is so when the player is in range, he stops but keeps looking at the AI to attack.
 
                     AttackTimer += 1 * Time.deltaTime;
 
                     if (AttackTimer >= AttackSpeed)
                     {
                         Attack();
                         AttackTimer = 0;
                     }
                 }
             }
         }
 
         if (AttackTimer < AttackSpeed)
         {
             playerAgent.stoppingDistance = 0;
         }
     }
 
     void Chase()
     {
         playerAgent.SetDestination(EnemyAttacking.transform.position);
         this is where i tell the player to follow the AI when attacking it.
     }
 
     void Attack()
     {
         Debug.LogFormat("{0} has delt {1}HP to {2}.", gameObject.name, AttackDamage, EnemyAttacking);
         EnemyAttacking.SendMessage("TakeDamage", AttackDamage);
         this is a function that ia have in another script, SendMessage is how i call it.
     }
 }

Comment
Add comment · 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

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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Apply Damage To Player On Collison With Specific Game Object 1 Answer

Deduct health on collision 2 Answers

Adding a counter? 1 Answer

Enemy animation help? 1 Answer

Enemy Health Problem 2 Answers


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