Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PingPong : MonoBehaviour {
  6.  
  7.     public float distance;
  8.     public float speed;
  9.     float originalZ;
  10.  
  11.     // Use this for initialization
  12.     void Start () {
  13.         originalZ = this.transform.position.z;
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.  
  19.         if (this.transform.position.z > originalZ + distance) {
  20.             speed *= -1;
  21.         }
  22.         if (this.transform.position.z < originalZ) {
  23.             this.transform.position = new Vector3(this.transform.position.x,
  24.                                                   this.transform.position.y,
  25.                                                   originalZ);
  26.             speed *= -1;
  27.         }
  28.         this.transform.Translate (Vector3.forward * speed * Time.deltaTime);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement