• 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
Question by sergeantironpig · Oct 06, 2017 at 09:47 AM · movementgameobjectobstacleupdown

How can i move a game object up and down so it acts like a obstacle for my sphere player

the object is a cube which has to go up and come down acting like a obstacle which requires timing to complete THANKS FOR YOUR HELP!!!!

Comment
dappledore
GGfazo

People who like this

2 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Dragate · Oct 06, 2017 at 10:24 AM

https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html

    float speed = 1f;
    float delta = 3f;  //delta is the difference between min y to max y.
 
    void Update() {
         float y = transform.position.y + Mathf.PingPong(speed * Time.time, delta); 
         Vector3 pos = new Vector3(transform.position.x, y, transform.position.z);
         transform.position = pos;
     }
Comment
mateov951
dappledore

People who like this

2 Show 7 · 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 sergeantironpig · Oct 06, 2017 at 11:28 AM 0
Share

@Dragate i didnot understand the mathf.pingpong part can u help me

avatar image Dragate sergeantironpig · Oct 06, 2017 at 11:35 AM 0
Share

Just like with the ping-pong ball (in the sports game), this function will bring back and forth y's value...from 0 to delta and when it reaches delta value it will start going back to 0 and all over again. We take advantage of that in y axis to make it go up and down.

It's just like Vector3.MoveTowards() @knorke suggested, but Mathf.PingPong() will take care of the the reverse movement as well.

avatar image sergeantironpig Dragate · Oct 06, 2017 at 11:43 AM 0
Share

sorry to bother you again but how to figure the offset @Dragate

Show more comments
avatar image eatsomechicken · May 31, 2018 at 10:08 PM 0
Share

I know that this thread has been here some time, but I have like the same problem. When I put your code in, it keeps telling me that there is an unexpected symbol ")". What is going on? Can u help me?

avatar image mateov951 · Jan 29, 2020 at 07:47 PM 0
Share

Made a few changes and it worked for me.

 void Update() {
     float y = Mathf.PingPong(speed * Time.time, delta);
     Vector3 pos = new Vector3(transform.position.x, y, transform.position.z);
     transform.position = pos;
 }

adding transform.position.y was creating an infinite movement towards y+

avatar image

Answer by knorke · Oct 06, 2017 at 10:17 AM

Use the MoveTowards function

 transform.position = Vector3.MoveTowards(firstPosition, endPosition, speed);
Comment

People who like this

0 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 sergeantironpig · Oct 06, 2017 at 10:19 AM 0
Share

thanks bro

avatar image

Answer by wizbit · Oct 06, 2017 at 11:39 AM

Hey

you can use a Co-Routine and use Vector3.Lerp

 IEnumerator move(Transform _transform)
     {
         startPos = _transform.position;
         t = 0;
 
             endPos = new Vector3(startPos.x + System.Math.Sign(input.x) *  startPos.y = System.Math.Sign(input.y), startPos.z);
         
 
         factor = 1f;
 
         while (t < 1f)
         {
             t += Time.deltaTime * moveSpeed * factor;
             _transform.position = Vector3.Lerp(startPos, endPos, t);
 
             yield return null;
         }
 
         yield return 0;
     }
Comment

People who like this

0 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 sergeantironpig · Oct 07, 2017 at 02:43 AM 0
Share

what do you mean by factor??? @wizbit

avatar image

Answer by Ylex · Mar 01, 2022 at 06:37 PM

What`s with "hi"? Is it cyber attack? ":)

Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

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

Related Questions

gameObject moves twice? 0 Answers

Strange movement behavior 0 Answers

OnMouseDown() Doesn't work 20 Answers

How to make object moving forward while looping up and down 0 Answers

Moving objects using raycasting? 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