I saw unity put out their own first person character controller(cc) so I was just playing around with it and wanted to make an elevator. I decided to try onPointerClick for the first time ever and I cant get it to work. That could be because I’m not understanding the intended use of it or my mine thought is that the unity cc turns “off” the mouse as far as clicking goes and that’s why it isn’t working. Either way I was wonder if anyone either knows why my approach isn’t working or knows a better way to be doing this? Here’s the code I’m using for the elevator. The UI button is supposed to call “callElevator()”.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Elevator : MonoBehaviour, IPointerClickHandler
{
Vector3 firstFloor;
Vector3 secondFloor;
public Transform top;
public bool isUp = false;
private void Start()
{
firstFloor = gameObject.transform.localPosition;
Debug.Log("First floor" + firstFloor);
secondFloor = top.localPosition;
Debug.Log("Second floor" + secondFloor);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
if (gameObject.transform.localPosition == firstFloor || gameObject.transform.localPosition == secondFloor)
{
Debug.Log(gameObject.transform.localPosition);
callElevator();
}
}
}
public void OnPointerClick(PointerEventData pointerEventData)
{
//Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
Debug.Log(name + " Game Object Clicked!");
}
public void callElevator()
{
if (!isUp)
{
StartCoroutine(elevatorUp());
isUp = true;
}
else
{
StartCoroutine(elevatorDown());
isUp = false;
}
}
public IEnumerator elevatorUp()
{
float totalMovementTime = 5f; //the amount of time you want the movement to take
float currentMovementTime = 0f;//The amount of time that has passed
while (Vector3.Distance(transform.localPosition, secondFloor) > 0)
{
currentMovementTime += Time.deltaTime;
transform.localPosition = Vector3.Lerp(firstFloor, secondFloor, currentMovementTime / totalMovementTime);
yield return null;
}
}
public IEnumerator elevatorDown()
{
float totalMovementTime = 5f; //the amount of time you want the movement to take
float currentMovementTime = 0f;//The amount of time that has passed
while (Vector3.Distance(transform.localPosition, firstFloor) > 0)
{
currentMovementTime += Time.deltaTime;
transform.localPosition = Vector3.Lerp(secondFloor, firstFloor, currentMovementTime / totalMovementTime);
yield return null;
}
}
}