Moving object up and down issue

So im trying to move my obstacle up and down so the player has to time it, but it makes some random movement so it goes like 10 up and 1 down, help please!

I haven´t quite understood the difference from Update and FixedUpdate, so if someone could explain that too it would be great!

#pragma strict

// Reference

private var player1 : GameObject;
private var playerstat : PlayerStat;
private var player2 : GameObject;
private var playerstat1 : PlayerStat1;


// Script
var Speed : float;
var Up = true;
public var UpNr : float;
public var DownNr : float;

function Start ()
{
    player1 = GameObject.FindGameObjectWithTag("Player1");
    player2 = GameObject.FindGameObjectWithTag("Player2");
    playerstat = player1.GetComponent(PlayerStat);
    playerstat1 = player2.GetComponent(PlayerStat1);
}

function FixedUpdate ()
{
    if (Up)
    {
        GoUp();
    }
    if (!Up)
    {
        GoDown();
    }
   
    {
        GetComponent.<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent.<Rigidbody>().velocity, maxSpeed);
    }
}

function GoUp ()
{
    GetComponent.<Rigidbody>().AddForce(0, UpNr, 0 * Speed);
    yield WaitForSeconds(1);
    Up = false;
}

function GoDown    ()
{
    GetComponent.<Rigidbody>().AddForce(0, DownNr, 0 * Speed);
    yield WaitForSeconds(1);
    Up = true;
}
var maxSpeed : float = 10.0;

function OnTriggerEnter(collision : Collider)
{
   if(collision.gameObject.tag == "Player1")
    {
    PlayerStat.Dead = true;
    }
   if(collision.gameObject.tag == "Player2")
    {
    PlayerStat1.Dead = true;
    }
}

Fixed update is called every 0.02 sec if i dont mistake (but it is fixed time)
Update is called when unity finishes calling all functions and stuff. (example update will be called less times if it has 20 functions than 5 functions)

i dont know much about add.force, i just use transform.position (i dont need more so why change :slight_smile:

can you give me an example of how you do with the transform.position then :)?
I need it to do the up and down in a smooth movement not teleporting up to down

Heh i dont know JS either :stuck_out_tongue:
but in C# it is best used as corutine

if (up)
{
   StartCorutine(Up);
}

then in up

IEnumator Up()
{
   Vector3 startPos = transform.position;
   Vector3 endPos = transform.position + new Vector3(0,2,0); //This is for moving 2 y up
   float overTime = 2f; //this means it moves to that position in 2 seconds
   float i = 0f;
   while (i <= overTime)
        {
            transform.position = Vector3.Lerp(startPos, endPos, i/overTime);
            i += Time.deltaTime;
            yield return null;
        }
    transform.position = endPos;
    up = false;

But as far as i know JS replace iEnumator with function and vectors and floats with var

same is for down but change endPos vector and up = true at end

hope it helps

Thanks for the help :slight_smile:
I´ve fixed the issue by using transform.Translate with time.deltaTime :slight_smile: