title: Unity3D - object moving up and down continuously within a y-axis range
body: <p>I have a target that moves up and down, but I'm not sure how to limit it's vertical movement to a certain y-axis range. Thanks for any advice. Code is as follows:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class TargetMovementVertical : MonoBehaviour 
  {
      public int maxSpeed;
  
      private Vector3 startPosition;
  
      // Use this for initialization
      void Start () 
      {
          maxSpeed = 3;
  
          startPosition = transform.position;
      }
  
      // Update is called once per frame
      void Update ()
      {
          MoveVertical ();
      }
  
      void MoveVertical()
      {
          transform.position = new Vector3(transform.position.x, startPosition.y + Mathf.Sin(Time.time * maxSpeed), transform.position.z);
  
          if(transform.position.y &gt; 1.0f)
          {
              transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
          }
          else if(transform.position.y &lt; -1.0f)
          {
              transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
          }
      }
  }
  </code></pre>