Skip to content

Instantly share code, notes, and snippets.

@ygjb
Created October 12, 2015 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygjb/ead9b5d8433c528425c3 to your computer and use it in GitHub Desktop.
Save ygjb/ead9b5d8433c528425c3 to your computer and use it in GitHub Desktop.
Elevator Scripts
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float moveSpeed = 3.0f;
public CharacterController myController;
void Start () {
myController = gameObject.GetComponent<CharacterController>();
}
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public bool isFlying = false;
void FixedUpdate()
{
if (myController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
Debug.Log("Jump");
}
if (Input.GetKeyDown(KeyCode.Tab))
{
isFlying = !isFlying;
}
}
moveDirection.y -= gravity * Time.fixedDeltaTime;
Debug.Log(moveDirection.ToString());
myController.Move(moveDirection * Time.fixedDeltaTime);
}
}
using UnityEngine;
using System.Collections;
public class ElevatorScript : MonoBehaviour
{
[SerializeField]
Transform platform;
[SerializeField]
Transform startTransform;
[SerializeField]
Transform endTransform;
[SerializeField]
float platformSpeed;
Vector3 direction;
Transform destination;
void Start()
{
SetDestination(startTransform);
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(startTransform.position, platform.localScale);
Gizmos.color = Color.red;
Gizmos.DrawWireCube(endTransform.position, platform.localScale);
}
void FixedUpdate()
{
// Normalized Vector pointing at target!
platform.GetComponent<Rigidbody>().MovePosition(platform.position + direction * platformSpeed * Time.fixedDeltaTime);
if (Vector3.Distance(platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime)
{
SetDestination(destination == startTransform ? endTransform : startTransform);
}
}
void SetDestination(Transform dest)
{
destination = dest;
direction = (destination.position - platform.position).normalized;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment