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 wenhua · Dec 19, 2011 at 06:33 AM · auto

i want to do a auto moving objects

function FixedUpdate() {

 // Move the character a bit each frame.
    transform.position += transform.forward * .02;
     
 // Destroy the character when it's out of view.
 if (Vector3.Distance(Vector3.zero,transform.position) > 20);
    transform.position -= transform.forward * .02;
         

}

i was working on this object where i need it to move 20 distance far away and make it auto move back to orignal places and repect the process which is 0-20 then 20-0 and continue... But this code will move to 20 and stop there or somethings it will move all the way can some 1 help me with it Thx

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by syclamoth · Dec 19, 2011 at 06:44 AM

The object stops at the end because you are telling it to rapidly oscillate around the end point- you never tell it to stop moving, so it keeps moving forward and moving backwards the same amount every frame.

A better way to do this would use coroutines to manage the movement.

function Start() { StartCoroutine(MoveBetweenWaypoints(transform.position, transform.position + transform.forward * 20, 20)); }

function MoveBetweenWaypoints(start : Vector3, end : Vector3, patrolTime : float) { while(true) { GoToPoint(end, patrolTime); GoToPoint(start, patrolTime); } }

function GoToPoint(target : Vector3, time : float) { var elapsedTime : float = 0; var startPos : Vector3 = transform.position; while(elapsedTime < time) { transform.position = Vector3.Lerp(startPos, target, time / elapsedTime); elapedTime += Time.deltaTime; yield; } }

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 wenhua · Jan 10, 2012 at 08:46 AM 0
Share

Isit somethings wrong with the script,it cant work for me T_T,althought i have find another way but,i want to solve this script

avatar image syclamoth · Jan 10, 2012 at 01:24 PM 0
Share

Sorry, I forgot a bracket there. It's not really that hard an error to pick up.

avatar image
0

Answer by wenhua · Dec 19, 2011 at 12:05 PM

thx i try see can work

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
avatar image
0

Answer by wenhua · Dec 20, 2011 at 06:47 AM

i try to copy and paste in but why these error or somethings with this

No approprite version of 'UnityEngine.MonoBehaviour.StartCoroutine'for the argument list '(void)' was found.

was there anythings need to change or what?

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

input .lzma too short 0 Answers

how can i manually update unity web player ? 0 Answers

Is there any way to create mountains by giving coordinates as inputs? 1 Answer

Unity Auto-update problem??? 1 Answer

Auto Translate after cloning gameobject using Instantiate() 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