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
1
Question by helloiam · Dec 05, 2012 at 10:13 PM · vector3randomobjectsmovingpositions

How to moving objects up and down slowly?

Hi, as you guys don't know, I am new to Unity3D but I am learning quikly, so complex code is welcome.

However, coming to the point, I have blocks (huge blocks like buildings) in my game as a background, what I need is to move the objects, if it can, move the blocks randomly up and down. So it's creating a nice effect int the background, but I don't want to animate it, because I am already instantiating and looping the blocks randomly in a size of x and y in the background.

Comment
Add comment · Show 3
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 helloiam · Dec 06, 2012 at 10:02 AM 0
Share

Thanks for your quik reaction, I have tested it and it won't work.

I think because I am working with C#. It asks by "transform.position.y = maxUpAndDown $$anonymous$$athf.Sin(angle toDegrees);" for an return type.

I hope you will help me further on :)

avatar image Dexter.Unity · Dec 06, 2012 at 11:55 AM 0
Share

You're welcome :)

I am not familiar with C# but I do know that you can simply make the script I wrote before in a Javascript and place it on the blocks. I placed it on a simple cube and it worked as well:)

So eventhough you use C# in all your other scripts you can still make one in JavaScript without your game failing or anything

avatar image helloiam · Dec 06, 2012 at 02:53 PM 0
Share

$$anonymous$$y friend you helped me out! I figured it out, I just used a Vector3 to assign the transform and gave it a return value and, then it worked. $$anonymous$$ay I know, where you have learned scripting in Unity and how many years it takes to be a proffesional in it (I know every day you practise you will be more advanced), but just to be good at it and knowing how to get things done. Do you also need to be a $$anonymous$$ath genious? And as last I am sorry in asking these questions, because this was not the subject what I have posted, but I will appriciate if you will answer these questions that I asked. Thanks!!!

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Dexter.Unity · Dec 05, 2012 at 10:52 PM

Here you go! Just adjust the vaues to yourliking ;)

Good luck!

 var maxUpAndDown             : float    = 1;             // amount of meters going up and down
 var speed                     : float = 200;            // up and down speed
 protected var angle            : float = 0;            // angle to determin the height by using the sinus
 protected var toDegrees     : float    = Mathf.PI/180;    // radians to degrees
 
 function Update()
 {
     angle += speed * Time.deltaTime;
     if (angle > 360) angle -= 360;
     transform.position.y = maxUpAndDown * Mathf.Sin(angle * toDegrees);
 }
Comment
Add comment · Show 6 · 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 Dexter.Unity · Dec 05, 2012 at 10:53 PM 0
Share

Please tell me if it worked :)

avatar image Dexter.Unity · Dec 05, 2012 at 11:05 PM 0
Share

You can also use FixedUpdate (){}.

This will make it's movement independent of the frame rate.

Should in this case give a more logical movement of the background blocks .

avatar image himanshu619 · Dec 06, 2012 at 03:39 PM 0
Share

Hey Dexter, How to set the position of moving gameobject along which it would move (i.e.y-axis).

avatar image Dexter.Unity · Dec 07, 2012 at 09:51 AM 0
Share

I don't completely understand. The transform.position.y should only adjust your opjects yvalue But if you want to locally trans form use the following:

transform.localPosition.y = maxUpAndDown $$anonymous$$athf.Sin(angle toDegrees);

avatar image himanshu619 · Dec 07, 2012 at 10:05 AM 0
Share

I want to know that how to set a particular position for y coordinate about which the gameobject will move (up/down). Coz' now it move around one particular point. How can we define it position! Particular for y-coordinate!

Show more comments
avatar image
3

Answer by Dexter.Unity · Dec 07, 2012 at 12:18 PM

Here u go!

Only when you adjust the MaxUpAndDown you have to reload the game

 var maxUpAndDown            : float = 1;            // amount of meters going up and down
 var speed                    : float = 50;            // up and down speed
 
 protected var angle            : float = -90;             // angle to determin the height by using the sinus
 protected var toDegrees        : float = Mathf.PI/180;    // radians to degrees
 protected var startHeight     : float;                // height of the object when the script starts
 
 
 function Start()
 {
     startHeight = transform.localPosition.y;
 }
 
 function FixedUpdate()
 {
     angle += speed * Time.deltaTime;
     if (angle > 270) angle -= 360;
     Debug.Log(maxUpAndDown * Mathf.Sin(angle * toDegrees));
     transform.localPosition.y = startHeight + maxUpAndDown * (1 + Mathf.Sin(angle * toDegrees)) / 2;
 }
Comment
Add comment · Show 4 · 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 Dexter.Unity · Dec 09, 2012 at 01:24 AM 0
Share

Did it work?

avatar image himanshu619 · Dec 12, 2012 at 04:09 PM 0
Share

Thanks Dexter it really work, But the only problem with this is that the player won't come done along with the gameobject smoothly, it stays in the air for some time and then come down. How could we fix the movement of player with up-down gameobject while payer it onto it.

avatar image Dexter.Unity · Dec 13, 2012 at 08:20 AM 0
Share

How is your player 'bound' to the gameobject? By gravity? or do you give your player the transform.position.y = gameobject height + object.transform.height?

avatar image Dexter.Unity · Dec 13, 2012 at 11:22 PM 0
Share

Can you otherwise make a website preview of your game and show it?

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

12 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

Related Questions

Can't get occlusion culling to work 4 Answers

C# Array with random.range help? 2 Answers

My assets losing colors when running on lgsvl simulator. Everything gets black and white after 20-30 minutes. Scripting and assets and maps were done on unity. any suggestions ? 0 Answers

random object creation and there movement 0 Answers

How to use mesh collision for moving external object? 0 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