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 Javyer · Jun 01, 2015 at 11:54 PM · objectnewbiemovetowards

Make object move towards position without stopping

Hello! I want to make the objects from Roll-A-Ball tutorial to move towards the direction of the player when they spawn, but my problem is that when they move to the last position, they stop there, I want them to continue once they start moving. How can I solve this?

Here is my code:

 public class Rotator : MonoBehaviour {
     private GameObject mytarget;
     private Transform targetPos;
     public float mySpeed;
     private Vector3 playerPos;
     public float speed;
 
     void Start () {
         mytarget = GameObject.FindGameObjectWithTag("Player");
         playerPos = mytarget.transform.position;
         mySpeed = 2;
     }
     void Update () {
         transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
         var step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, playerPos, step);
     }
 }

Thanks to whoever helps me!

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 YoungDeveloper · Jun 01, 2015 at 11:59 PM

Because you save the starting player position in a Vector3, which is a copy of those values and is not updated. Note that you will also have to fix the targets y before applying.

  private Transform target;
  private float speed = 2f;
  private Vector3 speedRot = Vector3.right * 50f;
  
  void Start () {
      target = GameObject.FindGameObjectWithTag("Player").transform;
  }
 
  void Update () {
      transform.Rotate (speedRot * Time.deltaTime);
      transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
  }

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 Javyer · Jun 02, 2015 at 04:06 PM 0
Share

That would make the object move toward the player position all the time, I'm trying to make it move in the direction the player was when the object spawned.

avatar image YoungDeveloper · Jun 02, 2015 at 05:49 PM 0
Share

Create states or enable/disable the script.

 private enum STATE{
     $$anonymous$$OVE,
     IDLE
 }
 
 private STATE state = STAT$$anonymous$$$$anonymous$$OVE;
 
 private void setState(STATE state){
     this.state = state;
 }
 
 private void Update(){
     if(state == STAT$$anonymous$$$$anonymous$$OVE){
         transform.Rotate (speedRot * Time.deltaTime);
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime);
 
         if(transform.position == target.position){
             setState(STAT$$anonymous$$IDLE);
         }
     }
 }




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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get a 'glow/highlight' effect on a object 1 Answer

New to Unity - Object Snapping? 1 Answer

Why isn't my object position (transform.position) updating? 1 Answer

Trying to create a '2D 8 Ball Pool' game - Trajectory Prediction 1 Answer

To move an object in an elliptical path. 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