Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by djordr · Mar 25, 2018 at 01:53 AM · playeraienemymovetowards

Enemy Chasing player?

I'm having an enemy move back and forward and then when he detects the player entering a cube that is parented to him I want him to face the player and move towards the player. This is going to make him go from walking to running hence why his speed is increased. When the player leaves the bound I want the enemy to return back to his original location and start moving forward and backward again.

The problem is that when the player enters the enemy's space and he faces the player. For some reason he decides to freeze for a second or two then start moving towards the player at great speed then after awhile freeze again and then start moving towards the player again at great speeds. What am I missing, I should be using OnCollisionStay right?

EDIT: I just noticed that I had to turn "one" and "two" bools off. I suppose I could ask instead how I would make him return to his original location. Would you suggest making the enemy a parent of a cube and move him back towards the cube and when he hits the cube make his rotation the same rotation as the cube and then turn the one bool on again?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour {
 
     Rigidbody enemyRigidBody;
 
     bool enemyIsMoving;
     public float enemySpeed = 2f;
     bool one;
     bool two;
     public float ToTarget;
     public static float distanceFromTarget;
     public Transform Player;
 
     void Start()
     {
         GetComponent<Transform>();
         GetComponent<Rigidbody>();
         one = true;
         enemyIsMoving = true;
         StartCoroutine ("movementTime");
     }
 
     void FixedUpdate()
     {
         if (one == true) {
             transform.Translate (Vector3.forward * enemySpeed * Time.deltaTime);
         } else if (two == true) {
             transform.Translate (Vector3.back * enemySpeed * Time.deltaTime);
         }
 
     }
 
     IEnumerator movementTime()
     {
         while (enemyIsMoving) {
             yield return new WaitForSeconds (5);
             one = false;
             two = true;
             yield return new WaitForSeconds (5);
             one = true;
             two = false;
         }
     }
 
     void OnTriggerStay(Collider thePlayer)
     {
         if (thePlayer.gameObject.tag == "Player") {
             Debug.Log ("1");
             enemyIsMoving = false;
             ChasePlayer ();
         } 
 
     }
 
     void OnTriggerExit(Collider thePlayer)
     {
         if (thePlayer.gameObject.tag == "Player") {
             enemyIsMoving = true;
             enemySpeed = 2;
         } 
 
     }
 
     void ChasePlayer()
     {
         enemySpeed = 5;
         transform.LookAt (Player);
         transform.Translate (Vector3.forward * enemySpeed * Time.deltaTime);
     }
 
 }
 
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

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

190 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

Related Questions

How to get in enemy to stop after a certain distance from the player? 0 Answers

Enemy 2D check for collision 1 Answer

What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers

Error CS12526, help! 1 Answer

I need the Nav Mesh Stop , I need the Animation to Play Javascript 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