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 $$anonymous$$ · Jan 27, 2014 at 08:33 PM · c#movementgameobjectmovefollow

Gameobjects move towards a moving object c#

I'm trying to get the following gameobjects to move towards another (my player), the only problem is with the following code, they only move towards its starting position. Where as I want them to be pulled towards where ever the player currently is. Any help would be appreciated.

 public Transform player;
     float speed = 5.0f;
 
     void Update () {
         if (PlayerScript.pulltime > 0)
         transform.position = Vector3.MoveTowards(transform.position, player.position, speed*Time.deltaTime);
 
     }

The Players movement script is as follows:

 void moveship ()
     {
         if (Input.GetKey (KeyCode.UpArrow))
             moveshipUp ();
         if (Input.GetKey (KeyCode.DownArrow))
             moveshipDown ();
         if (Input.GetKey (KeyCode.RightArrow))
             moveshipLeft ();
         if (Input.GetKey (KeyCode.LeftArrow))
             moveshipRight ();
     }
     void moveshipUp ()
     {
         if (this.transform.position.y < 3.4) {
             this.transform.Translate (Vector3.up * speed * Time.deltaTime, Space.World);
         }
     }
     void moveshipDown ()
     {
         if (this.transform.position.y > -4.388) {
             this.transform.Translate (Vector3.down * speed * Time.deltaTime, Space.World);
         }
     }
     void moveshipLeft ()
     {
         if (this.transform.position.x < 8.23) {
             this.transform.Translate (Vector3.right * speed * Time.deltaTime, Space.World);
         }
     }
     void moveshipRight ()
     {
         if (this.transform.position.x > -8.284) {
             this.transform.Translate (-Vector3.right * speed * Time.deltaTime, Space.World);
         }
     }

It should be following it's updated movement, but it doesn't.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MagicoCreator · Jan 27, 2014 at 08:44 PM

try setting player as a GameObject and access it's position by player.transform.position

Comment
Add comment · Show 7 · 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 $$anonymous$$ · Jan 27, 2014 at 08:45 PM 0
Share

I'm afraid it has the same effect.

avatar image MagicoCreator · Jan 27, 2014 at 09:15 PM 0
Share

well i just tried your script and it works as it is by me, so the modification i proposed is totally useless, it is tracking current position and moving towards correctly, if it doesn't work you are maybe setting the transform wrong i.e. instanciating a transform value yourself and filling it with static values, or that your player moves by animation whose movement isn't baked into the pose, and the original position of your character doesn't change

avatar image $$anonymous$$ · Jan 27, 2014 at 09:28 PM 0
Share

I've added the player movement script above. Sorry to keep asking, but do you see anything there which could be causing this? The Player is just a prefab with the movement script attached.

avatar image MagicoCreator · Jan 27, 2014 at 09:46 PM 0
Share

the movement script moves the transform so it shouldn't be a problem, unless your ship is parented to the player object and, you target your main object into the other object's script which doesn't move

avatar image $$anonymous$$ · Jan 27, 2014 at 10:07 PM 0
Share

Hmmm no it's just a single prefab, no children or parenting. :/

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Moving GameObject to various position ? 1 Answer

transform.Translate doesnt travel the same distance for multi-reso 2 Answers

How to make players follow only the movement but not the position / vector. 0 Answers

c# array of game objects movement help 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