make objects slowly go up, then slam down
as the title says im looking for a way to make a cube shaped object slowly rise up, then slam back down. i know the basics to make it move forward and back, bit thats about it
Is this just a visual thing or do you have physics involved somehow?
Answer by clunk47 · Aug 07, 2013 at 12:37 AM
Well you weren't too specific, so I'll just give you a basic tutorial on transform.Translate, in C-sharp, since you also didn't specify the language you want to use. This will still get you going.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
float speed;
Vector3 direction;
float min;
float max;
float units = 2.0f;
void Start()
{
//Set max to CURRENT position on Y axis, and set min to current minus units (2.0 in this example).
max = transform.position.y;
min = transform.position.y - units;
//Set first direction to be down
direction = Vector3.down;
}
void Update()
{
//Change speed depending on direction of object translation.
if(direction == Vector3.down)
{
speed = 10.0f;
}
else if(direction == Vector3.up)
{
speed = 1.0f;
}
//Use transform.Translate to move the current direction and current speed.
transform.Translate(direction * speed * Time.deltaTime);
//Change direction if object has reached min or max position on the Y axis (up and down).
if(transform.position.y >= max)
{
direction = Vector3.down;
}
if(transform.position.y <= min)
{
direction = Vector3.up;
}
}
}
this is perfect the way it is :) thankyou!
ill try to be better next time and explain more, it was late and i was tired lol
thanks for the help me it really work good and i have one another question if i click the mouse cursor in the game at run time so an object will be down so how it is possible please give me a reply... and give a code of this question..