Skip to content

Instantly share code, notes, and snippets.

@nicloay
Last active August 29, 2015 13:57
Show Gist options
  • Save nicloay/9615662 to your computer and use it in GitHub Desktop.
Save nicloay/9615662 to your computer and use it in GitHub Desktop.
Unity3d lifting platform (no lag) dont use it!!! see https://gist.github.com/nicloay/9662874
using UnityEngine;
using System.Collections;
public class LiftingPlatformController : MonoBehaviour {
public float routeLenght = 10;
public float verticalSpeed = 10;
[Range (0,1)]
public float startRelativePosition=0;
public bool goUp;
Vector3 basePosition, remotePosition;
void Start(){
basePosition = transform.position;
remotePosition = getRemotePosition();
transform.position = Vector3.Lerp(basePosition, remotePosition, startRelativePosition);
updateVelocity();
}
Vector2 velocity;
void Update(){
if (goUp && (transform.position.y - remotePosition.y) > 0){
goUp = false;
updateVelocity();
} else if (!goUp && (transform.position.y - basePosition.y) <0){
goUp = true;
updateVelocity();
}
rigidbody2D.velocity = velocity;
}
void updateVelocity(){
velocity = new Vector2(0, goUp ? verticalSpeed : -verticalSpeed);
}
Vector3 getRemotePosition(){
return basePosition + new Vector3(0, routeLenght, 0);
}
void OnDrawGizmos(){
if (Application.isPlaying){
Gizmos.DrawLine(basePosition, remotePosition);
} else {
Vector2 boxSize = (collider2D as BoxCollider2D).size;
basePosition = transform.position;
remotePosition = getRemotePosition();
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