I'm trying to make a script that allows the player to open doors and chests and activating elevators, however I've bumped into a few problems, for some reason it can't find my player character. The other problem is that I've tried make the defined thingie for my player public and draged the player prefab there from the inspector while in-game and then tried activating the elevator to see if it works but nothing happends.
Also since I might use this script on quite a few of my objects in-game (for chests etc) I'm not sure if it's smart having it reading if the object is selected in every frame in case it needs to perform the interactive actions, does anyone know another way I could set the interactive part up?
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour {
public GameObject target;
public bool allowReset = false;
public bool elevator = false;
public Transform elevPointA;
public Transform elevPointB;
public bool door = false;
public bool loot = false;
public int maxDistance;
public int Speed;
private bool _selected = false; //is the player marking this object with the mouse
private Transform _player;
private Transform _myPosition;
private Transform _targetTransform;
/// <summary>
/// State 0 = Elevator PointA, Door closed, Loot closed
/// State 1 = Elevator PointB, Door open, Loot looted
/// State 2 = Elevator moving
/// </summary>
private int _state = 0;
void Awake() {
_targetTransform = target.transform;
_myPosition = transform; //this is this objects transform
elevPointA = transform;
elevPointB = transform;
}
void Start() {
GameObject fp = GameObject.FindGameObjectWithTag("Player"); //find the gameObject tagged Player
_player = fp.transform; //read gameObject Player as a transform
}
void OnMouseEnter() {
_selected = true;
renderer.material.SetColor ("_OutlineColor", Color.blue);
}
void OnMouseExit() {
_selected = false;
renderer.material.SetColor ("_OutlineColor", Color.black);
}
void Update() {
if(Input.GetMouseButtonUp(0)) {
if(_selected == true) {
if(Vector3.Distance(_player.position, _myPosition.position) > maxDistance) {
_state = 1;
if(elevator == true)
Elevator();
else if(door == true)
Door();
else if(loot == true)
Loot();
}
}
}
// stop the elevator and reset the button if the elevator has reached CheckPoint A
if(Vector3.Distance(_targetTransform.position, elevPointA.position) == 0) {
if(_state == 0) {
return;
}
else {
Speed = 0;
_state = 0;
}
}
// stop the elevator and reset the button if the elevator has reached CheckPoint B
if(Vector3.Distance(_targetTransform.position, elevPointB.position) == 0) {
if(_state == 1) {
return;
}
else {
Speed = 0;
_state = 0;
}
}
}
void Elevator() {
if(_state == 0) {
_targetTransform.Translate(Vector3.up * Speed * Time.deltaTime);
_state = 2;
}
else if(_state == 1) {
_targetTransform.Translate(Vector3.up * Speed *Time.deltaTime);
_state = 2;
}
}
void Door() {
}
void Loot() {
}
}
(I'm usually just doing graphics but have begun coding just recently so please excuse me if my script is a mess.)