- Home /
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.
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 :)
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
$$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!!!
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);
}
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 .
Hey Dexter, How to set the position of moving gameobject along which it would move (i.e.y-axis).
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);
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!
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;
}
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.
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?
Can you otherwise make a website preview of your game and show it?
Your answer
Follow this Question
Related Questions
Can't get occlusion culling to work 4 Answers
C# Array with random.range help? 2 Answers
random object creation and there movement 0 Answers
How to use mesh collision for moving external object? 0 Answers