Hello,
I want to make a controllable turret. The player can walk up to a trigger and a guncamera is being enabled and the player itself is being disabled. When you don’t want to shoot the turret any longer, you can press a key and the player is being enabled again, and the guncamera is being disabled.
I then made a script that supports this. I can walk up to the turret and start shooting but I cannot exit the turret again. Do you have an idea?
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunExit : MonoBehaviour {
public GameObject player;
public Transform getOutPosition; // Position where the player should be enabled when exitting.
public GameObject cam; // Camera for the turret.
public GameObject propModel; // A mesh that is being enabled when not using turret.
public bool isPlayerIn = false; // Check if in trigger.
// Use this for initialization
void Awake () {
if (!getOutPosition)
{
GameObject getOutPos = new GameObject("Get Out Position");
getOutPos.transform.SetParent(transform);
getOutPos.transform.localPosition = new Vector3(-1.5f, 0f, 0f);
getOutPos.transform.localRotation = Quaternion.identity;
getOutPosition = getOutPos.transform;
}
}
void Start ()
{
cam.SetActive(false);
propModel.SetActive(true);
}
// Update is called once per frame
// Walk in trigger and enables all the things that needs to be present.
void OnTriggerEnter (Collider other) {
if(other.tag == "Player")
{
isPlayerIn = true;
cam.SetActive(true);
propModel.SetActive(false);
player.SetActive(false);
if (Input.GetKeyDown(KeyCode.F)) //Here the player can press F and he will be controllable again, and the turret is being disabled.
{
player.transform.SetParent(null);
player.transform.position = getOutPosition.position;
player.transform.rotation = getOutPosition.rotation;
player.transform.rotation = Quaternion.Euler(0f, player.transform.eulerAngles.y, 0f);
cam.SetActive(false);
player.SetActive(true);
isPlayerIn = false;
propModel.SetActive(false);
}
}
}
}