mardi 1 septembre 2015

object motions bound by to points

I'm want to animate the movement of a piston of a cylinder. i set the max and min points of the motion, but how can i leave to max/min point when the piston reach the end points?

I use a if function containing the arguments. it works fine but i'm not sure how to define the else argument.

void Update ()
{
    if (transform.position.y > 0) {

        if (Input.GetKey (KeyCode.UpArrow))
            transform.Translate (Vector3.up * moveSpeed * Time.deltaTime);

        if (Input.GetKey (KeyCode.DownArrow))
            transform.Translate (-Vector3.up * moveSpeed * Time.deltaTime);

    } else if (transform.position.y < 3) {

        if (Input.GetKey (KeyCode.UpArrow))
            transform.Translate (Vector3.up * moveSpeed * Time.deltaTime);

        if (Input.GetKey (KeyCode.DownArrow))
            transform.Translate (-Vector3.up * moveSpeed * Time.deltaTime);
    } else {

        if (Input.GetKey (KeyCode.UpArrow))
            transform.Translate (Vector3.up*0);

        if (Input.GetKey (KeyCode.DownArrow))
            transform.Translate (Vector3.down*0);
    }
}

it works fine when i animate one border at the time, but when i combine them, the piston keeps moving outside the borders also.

my questions are:

  1. How do i best define the else condition(outside the borders)
  2. and how do i leave the endpoints?

Aucun commentaire:

Enregistrer un commentaire