- Home /
Can't determine how to disable certain objects.
GOAL: To press Y on my XBOX controller so I can toggle the enabled weapon. CONDITIONS: The weapons are meant to be interchangeable, so they operate independently of the main ship. CURRENT STATUS: The weapons both work exactly how I want them to when enabled/ disabled, but I'm unable to get a working toggle. Just in case it would help, I gave them tags "MainWeapon" and "HeavyWeapon". PREVIOUS ATTEMPTS: I've tried using GetComponent, GetComponentInChildren, FindWithTag, simply creating a public GameObject, and FindGameObjectWithTag.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerShipScript : MonoBehaviour
{
//Ship
public float yawSwitch;
public float pitchTorque;
public float rollTorque;
public float yawTorque;
public float pitchAxisVal;
public float rollAndYawAxisVal;
private float currentPitch;
private float currentYaw;
public float currentRoll;
public Rigidbody ship;
//Movement
public float forwardSpeed;
public float reverseSpeed;
public float thrustSpeed;
public float thrustRate;
public int thrustMax;
public int thrustMin;
public float actualSpeed;
//Camera
private float pitchCamVal;
private float yawCamVal;
private float pitchCamPost;
public Transform holder;
public Camera cockpit;
private float cockpitCameraAxis;
private float currentCamPitch;
private float currentCamYaw;
private Transform holderRot;
private GameObject mainWeapon;
//UI
public Text speedReadout;
private void Start()
{
mainWeapon = GameObject.FindWithTag("MainWeapon");
//movement
thrustSpeed = 0;
thrustMin = 0;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Joystick1Button3))
{
}
yawSwitch = Input.GetAxisRaw("LeftTrigger");
pitchCamVal = Input.GetAxis("LeftV");
yawCamVal = Input.GetAxis("LeftH");
pitchAxisVal = Input.GetAxis("RightV");
rollAndYawAxisVal = Input.GetAxis("RightH");
pitchCamPost = Mathf.Clamp(pitchCamVal, -1f, 0.2f);
currentPitch = ship.rotation.x;
currentYaw = ship.rotation.y;
currentRoll = ship.rotation.z;
//Camera
holder.localRotation = Quaternion.Euler(currentPitch + (pitchCamPost * 90), currentYaw + (yawCamVal * 90), currentRoll);
//Movement
if (yawSwitch == 1f)
{
ship.AddTorque(transform.right * -pitchAxisVal * pitchTorque);
ship.AddTorque(transform.up * rollAndYawAxisVal * yawTorque);
} else
{
ship.AddTorque(transform.right * -pitchAxisVal * pitchTorque);
ship.AddTorque(transform.forward * -rollAndYawAxisVal * rollTorque);
}
actualSpeed = Mathf.RoundToInt(thrustSpeed);
if (Input.GetKey(KeyCode.Joystick1Button5) && (thrustSpeed < thrustMax))
{
thrustSpeed += thrustRate * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Joystick1Button4) && (thrustSpeed > thrustMin))
{
thrustSpeed -= thrustRate * Time.deltaTime;
}
//UI
speedReadout.text = "SPEED: " + actualSpeed;
}
private void FixedUpdate()
{
//Movement
ship.transform.Translate(Vector3.forward * actualSpeed / 50f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LightPulseCannonScript : MonoBehaviour
{
//Shooting
public float fireMain;
public GameObject shotPulse;
public Transform barrelLeft;
public Transform barrelRight;
public float fireRate;
public float nextFireLeft;
public float nextFireRight;
private bool alternate;
private Quaternion spread;
public float spreadVal;
public int rightAmmo;
public int leftAmmo;
public float ammoDelay;
public float ammoRechargeLeft;
public float ammoRechargeRight;
public float lastFireTimeLeft;
public float lastFireTimeRight;
public float ammoRechargeRate;
//UI
public Slider sliderUpper;
public Slider sliderLower;
public Image upperToggle;
public Image lowerToggle;
private void Awake()
{
//UI
upperToggle.enabled = false;
lowerToggle.enabled = true;
sliderUpper.value = leftAmmo;
sliderLower.value = rightAmmo;
}
private void Start()
{
//STARTUP CONTROL
enabled = true;
//SHOOTING
alternate = false;
fireRate = 0.09f; //Preferred value
spreadVal = 0.675f; //Preferred value
leftAmmo = 20; //Preferred value
rightAmmo = 20; //Preferred value
ammoDelay = 1.9f; //Preferred value
ammoRechargeRate = 0.2f; //Preferred value
}
private void Update()
{
//SHOOTING
fireMain = Input.GetAxisRaw("RightTrigger");
if (fireMain == 1f && Time.time > nextFireRight && alternate == true && leftAmmo > 0)
{
nextFireLeft = Time.time + fireRate;
spread = Quaternion.Euler(Random.Range(-spreadVal, spreadVal), Random.Range(-spreadVal, spreadVal), 0f);
Instantiate(shotPulse, barrelLeft.position, barrelLeft.rotation * spread);
alternate = false;
leftAmmo = leftAmmo - 1;
lastFireTimeLeft = Time.time + ammoDelay;
//UI
upperToggle.enabled = false;
lowerToggle.enabled = true;
}
if (fireMain == 1f && Time.time > nextFireLeft && alternate == false && rightAmmo > 0)
{
nextFireRight = Time.time + fireRate;
spread = Quaternion.Euler(Random.Range(-spreadVal, spreadVal), Random.Range(-spreadVal, spreadVal), 0f);
Instantiate(shotPulse, barrelRight.position, barrelRight.rotation * spread);
alternate = true;
rightAmmo = rightAmmo - 1;
lastFireTimeRight = Time.time + ammoDelay;
//UI
upperToggle.enabled = true;
lowerToggle.enabled = false;
}
if (Time.time > lastFireTimeLeft && Time.time > ammoRechargeLeft && leftAmmo < 20)
{
ammoRechargeLeft = ammoRechargeRate + Time.time;
leftAmmo = leftAmmo + 1;
}
if (Time.time > lastFireTimeRight && Time.time > ammoRechargeRight && rightAmmo < 20)
{
ammoRechargeRight = ammoRechargeRate + Time.time;
rightAmmo = rightAmmo + 1;
}
//UI
sliderUpper.value = leftAmmo;
sliderLower.value = rightAmmo;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LightSeekerLauncherScript : MonoBehaviour
{
//SHOOTING
public float fireMain;
public GameObject lightSeeker;
public Transform launcherLeft;
public Transform launcherRight;
public float fireRate;
public float nextFireLeft;
public float nextFireRight;
private bool alternate;
public int rightAmmo;
public int leftAmmo;
public float lastFireTimeLeft;
public float lastFireTimeRight;
//UI
public Image leftToggle;
public Image rightToggle;
public Text seekerAmmo;
private void Awake()
{
//UI
rightToggle.enabled = false;
leftToggle.enabled = true;
leftAmmo = 6;
rightAmmo = 6;
seekerAmmo.text = "" + (leftAmmo + rightAmmo);
}
private void Start()
{
//STARTUP CONTROL
enabled = false;
//SHOOTING
alternate = false;
}
private void Update()
{
fireMain = Input.GetAxisRaw("RightTrigger");
//SHOOTING
if (fireMain == 1f && Time.time > nextFireRight && alternate == true && leftAmmo > 0)
{
nextFireLeft = Time.time + fireRate;
Instantiate(lightSeeker, launcherLeft.position, launcherLeft.rotation);
alternate = false;
leftAmmo = leftAmmo - 1;
//UI
rightToggle.enabled = false;
leftToggle.enabled = true;
}
if (fireMain == 1f && Time.time > nextFireLeft && alternate == false && rightAmmo > 0)
{
nextFireRight = Time.time + fireRate;
Instantiate(lightSeeker, launcherRight.position, launcherRight.rotation);
alternate = true;
rightAmmo = rightAmmo - 1;
//UI
rightToggle.enabled = true;
leftToggle.enabled = false;
}
//UI
seekerAmmo.text = "" + (leftAmmo + rightAmmo);
}
}
Comment