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 Zexstoi · May 08, 2019 at 05:51 PM · movementaienemy

Unity AI/Enemy won't move towards target

Hello programmers around the world. I've been developing an AI for enemy and so far I have him wandering around. He can also detect the player but when that happens he should start moving towards him and he doesn't for some reason only his animation activates. Also when I get out of his fieldOfView or line of sight he returns to just wandering around. I only need to fix the moving towards the player issue.

Here is the code

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyScript : MonoBehaviour { [SerializeField] private EnemyScript enemy; private int randomNumber; [SerializeField] private Transform respawnPoint; [SerializeField] private float walkSpeed; [SerializeField] private float runSpeed; [SerializeField] private Animator eAnim; [SerializeField] private Transform player; // Use this for initialization void Start () { enemy.transform.GetChild(5).SetParent(null); } // Update is called once per frame void Update () { StartCoroutine(EnemyIdle(player)); FollowTarget (player); } private IEnumerator EnemyIdle(Transform player) { Transform target = respawnPoint.GetChild(randomNumber); Vector3 targetDir = target.position - transform.position; float step = walkSpeed * Time.deltaTime; Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F); transform.rotation = Quaternion.LookRotation(newDir); eAnim.SetBool("walk", true); transform.position = Vector3.MoveTowards(transform.position, target.position, walkSpeed * Time.deltaTime); if(Vector3.Distance(transform.position, target.position) < 0.01f) { eAnim.SetBool("walk", false); } yield return new WaitForSeconds(7); if (Vector3.Distance(transform.position, target.position) < 0.01f) { randomNumber = UnityEngine.Random.Range(0, respawnPoint.childCount); } } private void FollowTarget(Transform player){ float dist = Vector3.Distance(player.position, transform.position); if (dist < 10.0f){ StopAllCoroutines (); // get the target direction: Vector3 targetDir = player.position - transform.position; targetDir.y = 0; // kill any height difference to avoid tilting transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDir), 3.0f * Time.deltaTime); if (dist > 2){ // check min distance // only move to the target if farther than min distance // THIS IS THE THAT DOESN"T WORK FOR SOME REASON??? transform.position += transform.forward * walkSpeed * Time.deltaTime; } } } }

If anybody has any tips I would greatly appreciate it.

Comment
Add comment · Show 2
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 Zexstoi · May 08, 2019 at 05:52 PM 0
Share

Lol I don't know why the code is scrambled like this.

avatar image Magso Zexstoi · May 08, 2019 at 06:09 PM 0
Share

Paste your code in, then highlight it and then click the '101' button.

0 Replies

· Add your reply
  • Sort: 

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

218 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

Related Questions

Enemy Spaceship Movement AI Curve Movement Need help Please 0 Answers

Enemy following Player on uneven surface 1 Answer

Unity2D, How do i check which way a sprite is facing? 1 Answer

enemy AI patrol not working 1 Answer

AI controller script, Enemy movement issues 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