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 /
  • Help Room /
avatar image
0
Question by toonspanenburg · Jan 21, 2018 at 09:13 PM · 2d-platformer

My enemy doesn't follow the player. What did I do wrong?,My enemy does not move, he should walk to the player but he doesn't

public void Follow() { if (target.name.ToString()==("Player")) { Debug.Log ("lksfn"); Vector3 dir = target.position - transform.position; // z value is er gewoon omda het 2d is wete wel dir.z = 0.0f; if (dir != Vector3.zero) transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.FromToRotation (Vector3.right, dir), rotationSpeed * Time.deltaTime);

     transform.position += (target.position - transform.position).normalized 
         * moveSpeed * Time.deltaTime;
 }

}

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 shadowpuppet · Jan 21, 2018 at 09:30 PM 0
Share

Is there a Nav$$anonymous$$eshAgent involved here? I don't follow what you have. I just set the enemy's Nav$$anonymous$$eshAgent to go after the player

 using UnityEngine;
 using System.Collections;
 
 public class chasePlayer : $$anonymous$$onoBehaviour {
 Nav$$anonymous$$eshAgent agent;
 public Transform player;
 void Start () {
         
         agent = GetComponent<Nav$$anonymous$$eshAgent>();
 void Update(){
 agent.SetDestination (player.transform.position);
 
 }
 }
avatar image toonspanenburg shadowpuppet · Jan 21, 2018 at 09:36 PM 0
Share

Nope that isn't involved :/. but I'll definetly google that tomorrow.

tnx :) :) :)

avatar image shadowpuppet toonspanenburg · Jan 21, 2018 at 10:43 PM 0
Share

navmesh agent is just a component to put on your enemy. The surface your are on ( the ground) has to be set to static - or at least Navigation static. and just "bake " and unity will place an area for the navmesh to travel. Areas you don't want the enemy to go ( like a wall) make that wall also navigation static Non Walkable. this will carve out a chunk in the navmesh where the enemy can't go. otherwise he will ignore the wall completely and go right through it. Pretty simple stuff but you can get more complicated with it. Like having layers where -depending on the navmesh walkable settings in your enemy navmesh component - some enemies can go and others can't. i have a layer that only my flying drones can travel ( because it goes over water and valleys) Another layer where the drones can travel down hallways - but not go into rooms like the other enemies can. $$anonymous$$ind of fun stuff actually. I spent the day trying to figure out how to change layers at runtime. I have a draw bridge that I want enemies to cross, but only if the bridge is down. So I put that section of the navmesh on a new layer and named it "bridge" and no enemy had nav$$anonymous$$eshWalkable privilidges to it. Until the bridge is lowered then in code would set the walkable$$anonymous$$ask to include that layer so they can run across it

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shadowpuppet · Jan 22, 2018 at 02:48 AM

Here is a solution that works if you don't go by way of NavMeshAgent. Put it on any gameObject to test. You'll see that it has no regard for any obstacles that may get between him and the player - which is fine if it is a ghost and can go through walls. I also added a line at the end so the object stops when it gets too close otherwise it just circles around the player. One last thing is that - at least with my player - the pivot is on the ground , not in his center of mass, so the gameobject is coming at me looking at the ground. But he is moving and chasing and stops. NavMesh is a much better way

   using UnityEngine;
 using System.Collections;
 
 public class chasePlayer : MonoBehaviour {
 
     float moveSpeed = 3.0f;
     public GameObject player;//note the small case "p"
     private Transform Player;//note the upper case "P". You don't need this to make the object follow the player but I put it in in a hurry to do the last bit of code to get him to stop.
     public int stopDist = 2;
     
         void Start(){
         Player = player.transform;
         } 
         
         void Update(){
             //code for looking to player
             transform.rotation = Quaternion.Slerp(transform.rotation,
              Quaternion.LookRotation(player.transform.position - transform.position), moveSpeed * Time.deltaTime);
 
             
             //code for following the player
             transform.position += transform.forward * moveSpeed * Time.deltaTime;
 // the next part below  is what I through in to get the object to stop if too close then resume if player moves away again
         if (Vector3.Distance (transform.position, Player.position) <= stopDist)
             moveSpeed = 0f;
 if (Vector3.Distance (transform.position, Player.position) > stopDist)
                 moveSpeed = 3f;
         }
 }
 
 

Comment
Add comment · Show 1 · 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 toonspanenburg · Jan 22, 2018 at 01:59 PM 0
Share

Ah tnx for the help dude :) :) :)

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

78 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

Related Questions

Rigidbody2D not working 0 Answers

Having problems with Overlap Circle 0 Answers

2D platforms. Shoot towards mouse position 0 Answers

Why is my sprite only detecting one sprite as the ground? 1 Answer

2D Side scorller, moves but wont jump 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