• 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 · gameobjectmovementobstacleupdown

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

3 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

133 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

Related Questions

gameObject moves twice? 0 Answers

Make so the script only moves and makes actions to the gameObject that its attached to. 2 Answers

Moving different objects with randomized value 0 Answers

How to write a script which move the character when the it entered an area? 1 Answer

Player cant fall down 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges