Skip to content

Instantly share code, notes, and snippets.

@AchimTuran
Forked from nicloay/MobilePlatformController.cs
Last active August 29, 2015 14:12
Show Gist options
  • Save AchimTuran/65feb01aa0fed05c2af2 to your computer and use it in GitHub Desktop.
Save AchimTuran/65feb01aa0fed05c2af2 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class MobilePlatformController: MonoBehaviour {
public Vector2 route;
public float timePerCycle = 2.0f;
[Range (0,2)]
public float startRelativePosition=0;
float timeOffset = 0;
float routeLenght, horizontalSpeed;
Vector3 basePosition, remotePosition;
Vector3 normalizedRoute;
void Start(){
initProperties();
transform.position = Vector3.Lerp(basePosition, remotePosition, startRelativePosition);
previousPosition = transform.position;
}
Vector2 velocity;
Vector3 previousPosition;
void FixedUpdate(){
transform.position = basePosition + Mathf.PingPong((Time.timeSinceLevelLoad) * horizontalSpeed + timeOffset, routeLenght) * normalizedRoute;
rigidbody2D.velocity = (transform.position - previousPosition)/Time.deltaTime;
previousPosition = transform.position;
}
void initProperties(){
normalizedRoute = route.normalized;
routeLenght = route.magnitude;
horizontalSpeed = routeLenght/ timePerCycle;
if (startRelativePosition!=0)
timeOffset = routeLenght * (2 - startRelativePosition);
basePosition = transform.position;
remotePosition = basePosition + (Vector3)route;
}
void OnDrawGizmos(){
if (Application.isPlaying){
Gizmos.DrawLine(basePosition, remotePosition);
} else {
initProperties();
Vector2 boxSize = (collider2D as BoxCollider2D).size;
Gizmos.DrawLine(transform.position, remotePosition);
Gizmos.DrawCube(Vector3.Lerp(basePosition, remotePosition, startRelativePosition),boxSize);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment