Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MovingPlatform : MonoBehaviour
  5. {
  6.  
  7. public bool isPlayerOn;
  8.  
  9.  
  10. [SerializeField]
  11. Transform platform;
  12.  
  13. [SerializeField]
  14. Transform startTransform;
  15.  
  16. [SerializeField]
  17. Transform endTransform;
  18.  
  19. [SerializeField]
  20. float platformSpeed;
  21.  
  22. Vector3 direction;
  23. Transform destination;
  24.  
  25.  
  26. void OnCollisionEnter (Collision other) {
  27.  
  28. if (Collision.gameObject.tag == "Player") {
  29. isPlayerOn = true;
  30. }
  31.  
  32. }
  33.  
  34.  
  35. void OnCollisionExit (Collision other) {
  36.  
  37. if (Collision.gameObject.tag == "Player") {
  38. isPlayerOn = false;
  39. }
  40.  
  41. }
  42.  
  43. void Start (){
  44. SetDestination (startTransform);
  45. }
  46.  
  47.  
  48.  
  49. void FixedUpdate(){
  50. platform.rigidbody.MovePosition (platform.position + direction * platformSpeed * Time.fixedDeltaTime);
  51.  
  52. if (Vector3.Distance (platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime) {
  53. SetDestination (destination == startTransform ? endTransform : startTransform);
  54. }
  55. }
  56.  
  57.  
  58. void SetDestination(Transform dest){
  59. destination = dest;
  60. direction = (destination.position -platform.position).normalized;
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement