• 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 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
3
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
4

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 implement mobile controls 0 Answers

How to debug the object position in the grid of objects? 0 Answers

How to convert game object world position to hex grid cell coordinates? 1 Answer

Why Do My Obstacles Do This In My Object Pooler? 1 Answer

Multiple Cars not working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges