Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
0
Question by sabin22 · Sep 14, 2012 at 12:09 AM · movementmathfwhile

Is there a way to have an object gradually move to one spot, move back, and then stop?

Short version: Is there a way to have an object gradually move to one spot, move back, and then stop?

Long version: I'm trying to make an enemy for a game that moves along the ground and then hops up every two seconds. I've tried to use the Mathf.PingPong function, but I could not find a way to stop it once it gets going. Further, if I put the command in the Start() function, the object teleports to the destinations instead of gradually moving from one spot to the other.

It would be great if there was a Transform command that gradually moves an object to a designated spot, but my searches here and on the web have not turned up anything. If I had that, I could use "while" and "yield WaitForSeconds" to accomplish my AI movement. But the best I can do is have the objects teleport to a certain spot or have an object continuously move up and down.

Any ideas?

Here is my code for those who are curious:

 #pragma strict 
 
 //Stores the position the enemy spawns at 
 var initPos : Vector3; 
 var hopDirection : Vector2; 
 var hopSpeed : float; 
 
 function Start () {  
     initPos = transform.position;     
 } 
 
 function Update() { 
     transform.Translate(Time.deltaTime * -5, 0, 0); 
 } 
 
 function TumbleHop() {  
     transform.position.y = initPos.y + Mathf.PingPong(Time.time * hopSpeed, hopDirection.y);     
     transform.position.x = initPos.x + Mathf.PingPong(Time.time * hopSpeed, hopDirection.x); 
 }
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
Best Answer

Answer by Eric5h5 · Sep 14, 2012 at 12:13 AM

Use a coroutine, like MoveObject.

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 sabin22 · Sep 16, 2012 at 07:57 AM 0
Share

Works like a charm. Thank you, sir.

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

10 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

Related Questions

SmoothDamp won't reach its target 1 Answer

Trying to understand directional calculations 0 Answers

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders. 1 Answer

Player shaking when move (not the camera) 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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