Created
October 12, 2015 19:50
-
-
Save ygjb/ead9b5d8433c528425c3 to your computer and use it in GitHub Desktop.
Elevator Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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