Day 81 — Creating an Elevator in Unity

public MeshRenderer callButton;private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
callButton.material.color = Color.green;
}
}
}
private bool _goingDown = false;[SerializeField]
private Transform _origin, _target;
private float _speed = 2.5f;
public void CallElevator()
{
_goingDown = !_goingDown;
}
private void FixedUpdate()
{
if (_goingDown == true)
{
transform.position = Vector3.MoveTowards(transform.position, _target.position, _speed * Time.deltaTime);
}
else if (_goingDown == false)
{
transform.position = Vector3.MoveTowards(transform.position, _origin.position, _speed * Time.deltaTime);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = this.transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = null;
}
}
private Elevator _elevator;private void Start()
{
_elevator = GameObject.Find("Elevator").GetComponent<Elevator>();
if (_elevator == null)
{
Debug.LogError("The elevator is null");
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
callButton.material.color = Color.green;
_elevator.CallElevator();
}
}
}

--

--

--

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why NoSQL become increasingly popular?

Working with AWS Cloud from CommandLine — Part5

Git Survival Kit

Lawyers in Utah

lawyers in utah

Solve CRM to HubSpot CRM: Turn Your Business Success into Reality — Data2CRM

Simple Calculator In Python Using Functions

Code Snippet 1

Pokémon and Kubernetes: How are they connected?

Python Tutorial Series

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Connor Fullarton

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

More from Medium

Unity Tutorial — Toon Shade in HLSL

Animating Sprites in Unity

Cinematography in Unity — Working With Audio In Timeline

How to Build an Interactive Security Camera System in Unity