Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sureshshekar98 · Sep 05, 2018 at 06:04 PM · animationfps controllerattacking

How to stop enemy from bumping into the player model but have it attack the player?

Hello! I'm developing a fps game in which i have a player model and a zombie model. for some reasons the enemy model just keeps bumping or entering into my player model rather than attacking. I have set the enemy trigger on the fps controller and also made a collider trigger which has the script of Enemy AI to walk towards the player when in range and stay idle when out of range. The main zombie model is the child of this box collider of the zombie. what is the issue? is this something regarding the attack script or triggers or the child and parent of zombie thing? Please help me out in this issue. if possible please suggest me some solutions in my script, all the animations are marked as legacy. Do i need to change parent or child of zombie model? I have attached my script for the zombie AI and also the Screenshots of zombie anims and parent/child. .

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class ZombieEnemy : MonoBehaviour
 {
     
     public GameObject ThePlayer;
     public float TargetDistance;
     public float AllowedRange = 10;
     public GameObject TheEnemy;
     public float EnemySpeed;
     public int AttackTrigger;
     public RaycastHit Shot;
 
 
 
     void Update()
     {
         transform.LookAt(ThePlayer.transform);
         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
         {
             TargetDistance = Shot.distance;
             if (TargetDistance < AllowedRange)
             {
                 EnemySpeed = 0.05f;
                 if (AttackTrigger == 0) ;
                 TheEnemy.GetComponent<Animation>().Play("walk");
                 transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
             }
 
         }
        else 
         {
             EnemySpeed = 0;
             TheEnemy.GetComponent<Animation>().Play("idle");
         }
     
     
        if (AttackTrigger == 1)
         {
             EnemySpeed = 0;
             TheEnemy.GetComponent<Animation>().Play("attack");
 
         }
     }
 public void OnTriggerEnter()
     {
         AttackTrigger = 1;
     }
 public void OnTriggerExit()
     {
         AttackTrigger = 0;
     }
 }
   
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pruthv · Dec 13, 2018 at 09:33 AM

Well, there are some issues in your script. Firstly, none of your AttackTrigger == 1 lines are executed because you are changing EnemySpeed to 0, however in Update you are again changing it to 0.5f as TargetDistance < AllowedRange. Also, you are continuously playing "walk" animation as your if statement does not run anything because of the semicolon at the end of line. So, even if you change animation state to attack it will go back to walk in the immediate next update.

Most importantly, your zombie is moving exactly on the player position which is by default the exact center of the player. Zombie can not pass through player's body due to collider which is why it keeps bumping the player.

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

237 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 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 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 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 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 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 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 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 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 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

Can I make animations snap to a frame? 1 Answer

Combining two animations? 2 Answers

How to time damage with attacking animation 2 Answers

How to select an animation clip by index number? 7 Answers

What is the best/easiest way to add animations to a mesh I added to the FPSController 0 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