Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
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
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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
Add comment · 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.$$anonymous$$oveTowards() @knorke suggested, but $$anonymous$$athf.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

$$anonymous$$ade a few changes and it worked for me.

 void Update() {
     float y = $$anonymous$$athf.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
0

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

Use the MoveTowards function

 transform.position = Vector3.MoveTowards(firstPosition, endPosition, speed);
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 sergeantironpig · Oct 06, 2017 at 10:19 AM 0
Share

thanks bro

avatar image
0

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

what do you mean by factor??? @wizbit

avatar image
0

Answer by Ylex · Mar 01 at 06:37 PM

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

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

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

Object moves to 0,0,0? (Just want to know why it works) 1 Answer

Game Object not moving in Canvas 0 Answers

How to drag a game object with a mouse (along x axis)? 1 Answer

Vector3.Lerp not working? 5 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