Skip to content

Instantly share code, notes, and snippets.

@ygjb ygjb/Controller.cs
Created Oct 12, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.