Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Datsusara · Apr 30, 2014 at 12:11 AM · transformpositiongetcomponent

Can't get enemy to chase player

I am working on a 2D game in which there is a monster type who, upon instantiation, is supposed to move slowly toward the player. As the player moves and changes direction, the enemy should change course. The player is faster than the enemy, so staying ahead of it won't be a problem as long as the player is paying attention and there aren't too many enemies on the board.

All of the code for the player running, jumping, etc. works just fine, but the enemy isn't moving at all. Here is the code I have:

 using UnityEngine;
 using System.Collections;
 
 public class Monster3AI : MonoBehaviour {
     
     public Transform player;
     private Vector3 playerLocation;
     private float pursueSpeed = 200f;
 
     // Use this for initialization
     void Start ()
     {
 
     }
         
     
     // Update is called once per frame
     void Update () {
         player = GetComponent<Transform>();
         playerLocation = new Vector3 (player.position.x, player.position.y, player.position.z);
         Debug.Log ("playerLocation: "+playerLocation);
         transform.position=Vector3.MoveTowards(transform.position,playerLocation,pursueSpeed*Time.deltaTime);
     }
 }

This script is attached to a GameObject called Monster3 that is instantiated from time to time. In the inspector, the Player reference says "Player (Transform)", but when I look at the Debug Log, it's showing the location where the Monster3 object was instantiated. I know that using GetComponent() in the Update section is a big performance hit, but I was just trying to get the system to update with the latest location.

I've tried both MoveTowards and Lerp, but neither seems to work.

I'm sure I'm missing something obvious, but if anyone could help, I'd appreciate it!

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
0
Best Answer

Answer by RetepTrun · Apr 30, 2014 at 12:14 AM

  player = GetComponent<Transform>();


delete that line

your getting yourself

Comment
Add comment · Show 2 · 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 Datsusara · May 01, 2014 at 06:30 PM 0
Share

Thanks for this feedback, but now $$anonymous$$onster3 is only going to the initial location where the Player prefab was instantiated. Looking at the Inspector in Runtime, I also notice that, although Player is moving around, the values for transform.position aren't updating. How do I get $$anonymous$$onster3 to get an up-to-date value for Player's location?

avatar image RetepTrun · May 02, 2014 at 01:25 PM 0
Share

Set player in the inspector. it works i tested it.

Youve been overthinking this, you can just set the player once in the inspector

 using UnityEngine;
 using System.Collections;
 
 public class folowNob : $$anonymous$$onoBehaviour {
 
     public Transform player;
     //private Vector3 playerLocation;
     private float pursueSpeed = 3;
     
     // Use this for initialization
     void Start ()
     {
         
     }
     
     
     // Update is called once per frame
     void Update () {
         //player = GetComponent<Transform>();
         //playerLocation = new Vector3 (player.position.x, player.position.y, player.position.z);
         //Debug.Log ("playerLocation: "+playerLocation);
         transform.position=Vector3.$$anonymous$$oveTowards(transform.position,player.position,pursueSpeed*Time.deltaTime);
     }
 }
avatar image
0

Answer by Datsusara · May 02, 2014 at 06:50 PM

It's still only going to the location where the Player prefab initialized! I'm not sure what I'm doing wrong... When you say, "Set player in the inspector," you mean grab the Player prefab and drag/drop it onto the location in the inspector window where it says "Player" under Monster3AI, correct? I've done that, but it still only wants to go to the default location setting of the prefab and not the current location.

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

21 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

Related Questions

How do you get a component of a GameObject if you know only its position? 2 Answers

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

How to move an object to your current positon? 0 Answers

How to calculate the distance between specific side of an object and the closest to it surface's edge 1 Answer

Position of Instantiated Objects from a reference 1 Answer


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