• 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 PlatPlayz · Jan 11, 2018 at 02:04 PM · movementfollowdoor

Object moving forward before it starts functioning

Hello, I wanted to make an enemy come out of doors and then start following the player. The enemy already follows the player, but when I put it behind doors it just starts moving towards the player and comes trought the wall next to the door. So I wanted to ask if there is a way the enemy would come out of the door first and then start following the player. I would appreciate your help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dray · Jan 11, 2018 at 03:00 PM

Generic question, generic answer. ;) Define multiple states:

 public class EnemyScript: MonoBehaviour {
     private enum EnemyState {WalkingDoor, FollowingPlayer};  
 
     public Transform target; // your point in front of the door
     public float speed = 1f; // how fast the GameObject moves while exiting
     private EnemyState enemyState = EnemyScript.EnemyState.WalkingDoor; // current state
 
     void Update() {
         switch (enemyState ) {
             case EnemyScript.EnemyState.WalkingDoor:
                 // move towards the target
                 transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

                 // when close enough to the target, switch to following
                 if (Vector3.Distance(target.position, transform.position) < 0.1f) {
                     state = EnemyScript.EnemyState.FollowingPlayer;
                 }
             break;
 
             case EnemyScript.EnemyState.FollowingPlayer:
                 // run your follow-the-player-logic ...
             break;
         }
     }
 }

Comment
Add comment · Show 11 · 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 PlatPlayz · Jan 11, 2018 at 03:02 PM 0
Share

That's the problem, I'm not sure how the leaving-the-door-logic should look like.

avatar image Dray PlatPlayz · Jan 11, 2018 at 03:12 PM 0
Share

I see, okay so basically it sounds like what you need to do is:

  1. $$anonymous$$ove your object to a certain target over a certain time (some point straight in front of your door)

  2. Eventually run an animation on your enemy model and on the door(?)


This line of code moves a GameObject towards your target:

 Transform target; // <- you decide where this comes from, could also use a Vector3
 float speed = 1f;
 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime);


avatar image PlatPlayz Dray · Jan 11, 2018 at 03:19 PM 0
Share

I've made a small picture to show you exactly what I mean. As you see on the picture, the enemy spawns on a set position behind the door, moves straight forward, and then on a set position starts following the player.alt text

1.png (7.6 kB)
Show more comments
avatar image
1

Answer by NickBullseye · Jan 11, 2018 at 02:39 PM

Your question is quite incomplete. Is this a closed door, that your enemy has to open and come out of it? Or it is just an open doorway?

Theare a few ways to handle it. The most easiest, I suppose, is to set up your navigaition walkable areas, if it is not yet set, to prevent the enemy from walking through walls. (There are great unity tutorials on this topic).

If you door is closed and you want it to open, simly when the enemy comes in range with it, just set up trigger collider and when enemy comes in range with it just open it.

Hope this helped.

Edit: Wrote a short example of how to do it manually. It's kinda hardcodede but well, here it is:

So you will need atleast three objects: Player, Enemy, Door with tags set to each other corresponding to tags in code. Enemy should have a rigidbody for collision detection purposes. Door should have a child object placed after the door(point where the enemy will move to pass the door). Also you have to put a trigger collider before the door to trigger the behaviour.

Script you put on Enemy:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour {
 
     private Transform followTarget;//target we are currently folowing
     private bool isFollowing;//shows if we are following or not atm
     
     [SerializeField]private float speed = 2f;//speed of the enemy
     [SerializeField]private float doorOpenTime = 2f;//time when enemy stands still while door opens
 
 
     void Start() {
         followTarget = GameObject.FindGameObjectWithTag("Door").transform;//we store the door transform as our followTarget
         isFollowing = true;//start following
     }
 
     void Update() {
         if (isFollowing) {
             transform.position = Vector3.MoveTowards(transform.position, followTarget.position, speed * Time.deltaTime);//if we enemy is currently following something it move to it gradually
         }
     }
 
     public void WaitForDoorOpen() {
         isFollowing = false;//stop following
         Invoke("PassThroughDoor", doorOpenTime);//resume following when door animation ends
     }
 
     void PassThroughDoor() {
         followTarget = followTarget.GetChild(0).transform;//we get the point after the door which is a child object of the door
         isFollowing = true;//start following
         Invoke("StartFollowingPlayer", 4f);//after some time after we pass the door we start folowing player
     }
 
     void StartFollowingPlayer() {
         followTarget = GameObject.FindGameObjectWithTag("Player").transform;//store player transform as our follow target
     }
 
 }

And the code we put on the door:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Door : MonoBehaviour {
 
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Enemy") {//if the collider entered the trigger has tag enemy
             other.GetComponent<Enemy>().WaitForDoorOpen();//run the function
         }
     }
 }


Comment
Add comment · Show 4 · 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 PlatPlayz · Jan 11, 2018 at 02:57 PM 0
Share

Sorry, my question wasn't really clear. What I mean is that I want the enemy to spawn behind the door, come to it, it will open (I have an animation set up for it) , he will come through it, and only then start following the player.

avatar image NickBullseye PlatPlayz · Jan 11, 2018 at 03:08 PM 0
Share

If the door is openning by itself and the enemy is not playing any open animation I would do it this way:

Use the navigation system i mentioned above. Where door and all the ground is walkable and walls are not walkable. Enemy is going to build his route through the door and when he is co$$anonymous$$g throught it you will have to trigger the door open animation. You can stop your enemy for a sec, than resume following if u wish.

IF you dont like using NavSystem, the other way is to make your enemy first follow the door, than when it reached the door just switch for player.

avatar image PlatPlayz NickBullseye · Jan 11, 2018 at 03:13 PM 0
Share

I understood your idea, but I don't know how to make the enemy build his route trough the door. I just want it to spawn, move forward, and only when it reaches a certain point start following the player.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

115 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

Related Questions

Having Trouble animating camera at start of game 1 Answer

Smooth Camera Follow Script, Weird Movement... Please help! 1 Answer

Moving a Game Object in a specific volume/area 1 Answer

Camera rotation around player while following. 6 Answers

How to Follow By Getting KeyDown? 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