Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by lkarus · Aug 18, 2015 at 06:45 AM · c#scripting problempositionmove an object

Making a gameobject move to a position

So I have this game object that I want to move to a position.

What I do not want to do is to have a FixedUpdate function that has anything similar to

MovePosition(pos speed Delta time)

Is there a way to only call a function once, and it does the moving for you instead? I don't want it to teleport from point A to B so just setting the transform.position = newPos isnt something that I am looking for.

The only thing that I can think of is using a coroutine, but I was wondering if there is a function that guarantees that it moves to a point without me having to update its position every frame in the Update function.

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

Answer by Vice_Versa · Aug 18, 2015 at 07:02 AM

i dont know why youre avoiding the update function.. that seems strange to me but there are a few ways to do what youre asking.

you can make a new animation that moves one object to another then call animation.Play() on that gameobject

you could set up a while loop for example:

 float secondX;
 float secondy;
 float secondz;
 Vector3 firstPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
 Vector3 secondPosition = new Vecotr3(whatever, whatever, whatever);
 while(transform.position.x < secondX)
 {
   transform.position = new Vecotr3(transform.position.x + 0.001f, transform.position.y, trnasform.position.z);
 }
 

though, the above way is technically just you coding something similar to an update function

and yes, coroutines would also be another way.

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 lkarus · Aug 18, 2015 at 07:05 AM 1
Share

I'm not sure what thought process was going through me when I was writing this question. I also find it strange how I did not want to use the update function. I'm sorry for the inconvenience.

avatar image
8

Answer by Tom01098 · Aug 18, 2015 at 08:27 AM

According to http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html , the code you need is simply

 public Transform target;
     public float speed;
     void Update() {
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
     }

So, yes, it does have to be in an updating function to my knowledge, but it's a very simple code.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to increment value of axis when something happens 2 Answers

I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers

How can i place subjects in different positions? 0 Answers

Multiple Cars not working 1 Answer

How to Change the position of a tile to the player position? 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