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 RandomUser123 · Aug 17, 2014 at 12:20 PM · positionfollowmovetowards

Issue with enemies going towards player

alt text

I'm having a little issue with my enemies at the moment, I want my coloured balls to roll towards the player, but at the moment, they just seem to roll to the position in the photo, regardless of where my player is.

Does anyone know why this may be? Here is the code I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class moveTowardsPlayer : MonoBehaviour 
 {
     public Transform target; // drag the player here
     float speed = 10.0f; // move speed
     
     void Update()
     {
         transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
     }
 }

capture3.png (15.3 kB)
Comment
Add comment · Show 8
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 Alessio89 · Aug 17, 2014 at 01:05 PM 0
Share

That's odd, I won't lie. At first glance I can't see anything wrong with the script. Could you do some debugging? Place a Debug.Log("Player position: " + transform.position) on the player and a Debug.Log("Target position: " + target.position) on the enemy and make sure that indeed these two values are the same.

Or you could try and use Vector3.Lerp ins$$anonymous$$d of $$anonymous$$oveTowards and see if that helps. Let me know the results of the debugging :)

avatar image RandomUser123 · Aug 17, 2014 at 01:25 PM 1
Share

I tried the Debugs there and indeed something is wrong as the x position of the target is -15 :

alt text

Why would that be the case do you know? I'll try Vector3.Lerp now

capture3.png (14.8 kB)
avatar image Alessio89 · Aug 17, 2014 at 01:27 PM 0
Share

I suppose Lerp won't work either in this case. For testing purpose, can you pass a GameObject as target ins$$anonymous$$d of a transform, and use then Target.transform for the debugging? It's fishy, but we need to restrict the circle :)

Edit: Oh wait! Does the cube have a parent object? Like is the hierarchy "Player -> Cube" and you're passing one of these as a transform and they're not aligned? $$anonymous$$aybe the Cube is parented to a gameobject but the pivots (the return values of the transform.position) are offsetted? Could you check that?

avatar image Alessio89 · Aug 17, 2014 at 01:34 PM 1
Share

Ok yes, that could be the problem indeed. A workaround would be to get the player reference in the Start function of the enemies. You would do like this:

 using UnityEngine;
 using System.Collections;
  
 public class moveTowardsPlayer : $$anonymous$$onoBehaviour 
 {
     public Transform target; // drag the player here
     void Start()
     {
        target = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     float speed = 10.0f; // move speed
  
     void Update()
     {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime);
     }
 }

And then make sure that the cube has the tag "Player". That way, at the start of the scene, all the enemies will look for a Player and store that as a reference ins$$anonymous$$d of the prefab.

avatar image RandomUser123 · Aug 17, 2014 at 01:38 PM 1
Share

Brilliant, works great :) Thanks very much for your help Alessio. If you convert your comment to an answer, I will accept it :)

Show more comments

1 Reply

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

Answer by Alessio89 · Aug 17, 2014 at 01:39 PM

Since I suppose the question is answered, I'll post the complete answer here for anyone who might have the same problem:

The problem was that the enemies were referencing a Prefab and not the instanciated gameobject.

The solution was to attach the tag "Player" to the cube and then make sure the enemies were referncing it at the beginning of the scene like so:

 using UnityEngine;
 using System.Collections;
  
 public class moveTowardsPlayer : MonoBehaviour 
 {
     public Transform target; // drag the player here
     void Start()
     {
        target = GameObject.FindGameObjectWithTag("Player").transform;
     }
  
     float speed = 10.0f; // move speed
  
     void Update()
     {
         transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
     }
 }

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

24 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

Related Questions

Camera rotation around player while following. 6 Answers

How to move gameobject (main camera) to another gameobject (UI camera) position ? 1 Answer

MoveTowards sends object passed target 1 Answer

Having a 3D text appear in the middle of the screen without using GUI (C#) 2 Answers

Make player follow mouse position 3 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