Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

My Platform Mover (Not need Parenting)

Discussion in 'Scripting' started by Charles-Li, Jul 10, 2014.

  1. Charles-Li

    Charles-Li

    Joined:
    May 27, 2014
    Posts:
    9
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformMover : MonoBehaviour {
    5.  
    6.   public Vector3 targetOffset;
    7.   public bool syncCycle = true;
    8.   public float timespanPerCycle = 10.0f;
    9.   public float speed = 1.0f;
    10.  
    11.   Vector3 originalPosition;
    12.   Vector3 targetPosition;
    13.   Vector3 lastPosition;
    14.   float distance;
    15.  
    16.   // Use this for initialization
    17.   void Start()
    18.   {
    19.     originalPosition = transform.position;
    20.     targetPosition = transform.position + targetOffset;
    21.     distance = Vector3.Distance (originalPosition, targetPosition);
    22.   }
    23.  
    24.   // Update is called once per frame
    25.   void FixedUpdate()
    26.   {
    27.     float weight;
    28.  
    29.     if (syncCycle) weight = Mathf.Cos (Time.time / timespanPerCycle * 2.0f * Mathf.PI) * 0.5f + 0.5f;
    30.     else weight = (Mathf.Cos(Time.time * speed / distance * Mathf.PI) * 0.5f + 0.5f);
    31.  
    32.     transform.position = originalPosition * weight + targetPosition * (1 - weight);
    33.   }
    34.  
    35.   void OnCollisionEnter()
    36.   {
    37.     lastPosition = transform.position;
    38.   }
    39.  
    40.   void OnCollisionStay(Collision theCollision)
    41.   {
    42.     theCollision.transform.position += (transform.position - lastPosition);
    43.     lastPosition = transform.position;
    44.   }
    45. }
    46.  

    Well, please let me credit the people concerned, as the math's part I just copy somebody else 's example.
     
    Last edited: Jul 10, 2014
  2. Arunraj

    Arunraj

    Joined:
    Jul 4, 2012
    Posts:
    21
    hello, add some details what you are expecting from forum. So people around here can help you out.
     
  3. Charles-Li

    Charles-Li

    Joined:
    May 27, 2014
    Posts:
    9
    Hi

    I don't have any problem.

    I just want to share my example, as I search on google show that some people have problem that the character on moving platform do not move with it and just falling down.
     
  4. KholdStare2399

    KholdStare2399

    Joined:
    Jun 29, 2014
    Posts:
    3
    Thanks i found this useful. I had been using the one from the unify wiki http://wiki.unity3d.com/index.php/MovingPlatform
    but it stopped working for unknown reasons. Yours is working pretty well. Any chance you could add route support into it like the one on the wiki has?