Quantcast
Channel: Questions in topic: "vehicles"
Viewing all articles
Browse latest Browse all 123

How do I set a child to active only when the parent is active?

$
0
0
Hello, I checked elsewhere, but I couldn't seem to find a solution. I am creating a game where the player can either be on foot or use vehicles. I used the vehicle hop in/out code to get a start. My question is how do I activate/deactivate children of the vehicle? Right now I have a weapon that is its own separate Gameobject, attached to the vehicle as a child of the vehicle. In following the car code, the vehicle is only activated when the player is near it, but the turret is always active. How can I make the turret check to make sure it's parent is active? Any insight or pointers to tutorials would be awesome. Thanks Turret (Currently causes game to freeze) using UnityEngine; using System.Collections; public class PlayerAiming : MonoBehaviour { // Update is called once per frame void Update () { if( (GameObject.Find("Armor").GetComponent("MechMove") as MonoBehaviour).enabled == true) { // We are going to read the input every frame Vector3 vNewInput = new Vector3(Input.GetAxis("Horizontal_R"), Input.GetAxis("Vertical_R"), 0.0f); // Only do work if meaningful if(vNewInput.sqrMagnitude < 0.1f) { return; } // Set our rotation to represent the input vNewInput.Normalize(); float fHeading = Vector3.Dot(Vector3.right, vNewInput); Vector3 vNewRotation = transform.rotation.eulerAngles; vNewRotation.z = fHeading * 90.0f; // Adjust our rotation if we're on the bottom half of the input circle if(vNewInput.y > 0.0f) { vNewRotation.z = 180.0f - vNewRotation.z; } // Apply the transform to the object transform.rotation = Quaternion.Euler(vNewRotation); } } } Mech Code using UnityEngine; using System.Collections; public class Mech : MonoBehaviour { //vars to check if player can enter Armor public bool isPlayerVisible; public Transform player; public Transform Armor; public Transform exitPoint; public float range = 5; public float distance; // Update is called once per frame void Update () { distance = Vector2.Distance(transform.position,player.position); if(Input.GetKey(KeyCode.Space) && distance < range){ //Make player invisible (GameObject.Find("Armor").GetComponent("MechMove") as MonoBehaviour).enabled = true; player.gameObject.SetActive(false); //set the exit parent player.parent = exitPoint.transform; player.transform.localPosition = new Vector2(0f,0f); //parent player to Armor; exitPoint.parent = Armor.transform; exitPoint.transform.localPosition = new Vector2(0f,0f); //pick up from here isPlayerVisible = false; } else { if(Input.GetKeyUp(KeyCode.R)) { //set player invisable player.gameObject.SetActive(true); //set the exit parent player.transform.parent = null; //parent player to car exitPoint.parent = Armor.transform; isPlayerVisible = true; (GameObject.Find("Armor").GetComponent("MechMove") as MonoBehaviour).enabled = false; } } } }

Viewing all articles
Browse latest Browse all 123

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>