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 /
This question was closed Dec 04, 2014 at 05:04 AM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Prasanna · May 20, 2014 at 01:42 PM · c#movementlerpsmooth

Movement Script

Hello there,

Someone please explain this, i saw this code from unity answers. I want to to what is the use of this code. its working perfect, but i need to know how to use this..

using UnityEngine;

using System.Collections;

public class AIMove : MonoBehaviour

{

 public Vector3 Position_Two;

 IEnumerator Start()

 {
     Vector3 Position_One = transform.position;
     while(true)
     {
         yield return StartCoroutine (MoveObject(transform, Position_One, Position_Two, 3.0f));
         yield return StartCoroutine (MoveObject(transform, Position_Two, Position_One, 3.0f));
     }
 }

 IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) 
 {
     float i = 0.0f;
     float rate = 1.0f / time;
     while (i < 1.0f) 
     {
         i += Time.deltaTime * rate;
         thisTransform.position = Vector3.Lerp(startPos, endPos, i);
         yield return null; 
     }
 }

}

-Prasanna

Comment
Add comment · Show 2
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 Nerevar · May 20, 2014 at 01:48 PM 0
Share

Hi,

$$anonymous$$y guess would be a linear patrol between two points.

The object which the script is attached to will move to the position you set with Position_Two.

the last parameter of $$anonymous$$oveObject() is the duration of a one way trip.

avatar image Gruffy · May 20, 2014 at 02:11 PM 0
Share

Hey bud, I would agree with Nerevar, However, the usage is ubiquitous to the act of carrying out an execution outside of the Update function (effectively a type of threading in Unity Engine allowing one to carry out other tasks that do not encroach the update function during runtime).

This would be handy for a number of things, for example, carrying out a task that moved something from one spot to another. this might be best carried out away from Update() as it may only need to be accessed intermittently, as opposed to carrying out the task per Update frame() - this can be haeavy, maybe not for one or two things happening simultaneously, but when there`s a 100 or a 1000 or more things happening simultaneously, well your imagination doesn`t need pri$$anonymous$$g anymore, I imagine :) Hence, The coroutines. Overall, Nerevar is most likely right and this is some very basic patrol code that executes away from Update to leave that to handle things that require constant attention, with respect to things like player movement or camera movement, continual game logic etc. Anyway, hope that helps in some small way. Cheers bud Gruffy

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by MrVerdoux · May 20, 2014 at 02:08 PM

Everything is happening because of the coroutines.

Start gets called just after the game object is instantiated. Then it yields a coroutine. When you yield a coroutine, the code waits for it to end before it goes to the next line. When does a coroutine end? When it has reached the last line of its code. However a coroutine could yield a time (say 1 second), so the coroutine would get paused 1 second before being finished.

Yielding null is a special case. It will make the coroutine stop until the next frame, which is exactly the same that waiting for Time.deltaTime seconds.

Therefore, the MoveObject coroutine increases i in a loop until the condition is true. Then it doesn't yield anything so it simply ends. At that point your gameObject is at position 2. Then the process starts again only that this time you perp from position 2 to the origin.

Summarizing: in this script you set only the destination position, and your character/object/whatever will go there and back to its original position in 6 seconds until the end of ages.

Edit: check here for further detail.

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 Prasanna · May 21, 2014 at 04:50 AM 0
Share

Hey Guys thanks for your answers, its help me to know about coroutine.

-Prasanna

Follow this Question

Answers Answers and Comments

22 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

Related Questions

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

Smooth Camera/Object Movement 1 Answer

Why is my object not rotating smoothly ? C# 1 Answer

Why is the paralaxing shaky? (video examples) 2 Answers

Vector3.lerp causes gameobject to move thousands of pixels away from where it's mean to go 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