Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Vehicle Entry Script, What am I doing wrong?

Discussion in 'Scripting' started by The-Game-Master, Dec 8, 2014.

  1. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    For some reason, even when all of the variables are assigned, it doesn't switch the cameras. It doesn't even print the "got to the OnCollisionEnter() function!". What am I doing wrong?

    Helicopter Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Helicopter : MonoBehaviour {
    5.     public bool Hovering;
    6.     public float HoverY;
    7.     public bool AutoPilot = false;
    8.     private Vector3 Goto_Location;
    9.     public float speed = 0.08f;
    10.     public Transform Camera;
    11.     // Use this for initialization
    12.     void Start () {
    13.         Camera.active = false;
    14.         if (AutoPilot) {
    15.             Goto_Location = new Vector3 (Random.Range (transform.position.x - 100, transform.position.x + 100), transform.position.y, Random.Range (transform.position.z - 100, transform.position.z + 100));
    16.         }
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if (AutoPilot) {
    22.             if (transform.position == Goto_Location) {
    23.                 Goto_Location = new Vector3 (Random.Range (transform.position.x - 100, transform.position.x + 100), Random.Range (transform.position.y - 100, transform.position.x + 100), Random.Range (transform.position.z - 100, transform.position.z + 100));
    24.             } else {
    25.                 transform.position = Vector3.MoveTowards (transform.position, Goto_Location, speed);
    26.             }
    27.         } else {
    28.             if (Input.GetButton ("HelicopterY.positive")) {
    29.                 rigidbody.AddForce (Vector3.up * 3000);
    30.             }
    31.             if (Input.GetButton ("HelicopterX.negative")) {
    32.                 rigidbody.AddForce (transform.forward * 2000);
    33.             }
    34.             if (Input.GetButton ("HelicopterX.positive")) {
    35.                 rigidbody.AddForce ((transform.forward * -1) * 1800);
    36.             }
    37.             if (Input.GetButton ("HelicopterZ.positive")) {
    38.                 rigidbody.AddTorque (new Vector3 (0, 1000, 0));
    39.             }
    40.             if (Input.GetButton ("HelicopterZ.negative")) {
    41.                 rigidbody.AddTorque (new Vector3 (0, 1000, 0) * -1);
    42.             }
    43.             if (Input.GetButtonDown ("Hover")) {
    44.                 Hovering = !Hovering;
    45.             }
    46.             if (transform.rotation.x <= -20) {
    47.                 transform.rotation = Quaternion.Euler (new Vector3 (20, 0, 0));
    48.             }
    49.             if (transform.rotation.x >= 20) {
    50.                 transform.rotation = Quaternion.Euler (new Vector3 (20, 0, 0));
    51.             }
    52.             if (Hovering) {
    53.                 rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
    54.             }
    55.         }
    56.     }
    57.  
    58.     void PlayerGettingIn(){
    59.         Camera.active = true;
    60.         print ("Helicopter Recieved the message!");
    61.     }
    62.  
    63.     void PlayerGettingOut(){
    64.         Camera.active = false;
    65.         print ("Helicopter Recieved the message!");
    66.     }
    67. }
    Player Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player1 : MonoBehaviour {
    5.     private int Gold = 500;
    6.     private bool ShowMenu = false;
    7.     public Transform HalfWall;
    8.     private bool Placed = false;
    9.     private Transform preview;
    10.     private bool InVehicle = false;
    11.     private GameObject Vehicle;
    12.     public Transform Camera;
    13.     //public Vector3 new_Pos = (Random.Range (0, 10), 4, Random.Range(0, 10));
    14.     // Use this for initialization
    15.     void Start () {
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void FixedUpdate () {
    20.         if(Input.GetKeyDown (KeyCode.E)){
    21.             Debug.Log ("Got E as a key pressed!");
    22.             ShowMenu = !ShowMenu;
    23.             Debug.Log (ShowMenu);
    24.         }
    25.         if(InVehicle){
    26.             transform.position = Vehicle.transform.position;
    27.         }
    28.     }
    29.     // This enables the user to see stats and variables, such as how much cash or health they have
    30.     void OnGUI(){
    31.         GUI.Label (new Rect (0, Screen.height - 128, 128, 32), "Gold: " + Gold.ToString ());
    32.         if(ShowMenu){
    33.             ShowBuildMenu();
    34.         }
    35.     }
    36.     // This is the menu for building parts of the base.
    37.     void ShowBuildMenu(){
    38.         if(GUI.Button (new Rect(0, 0, 128, 32), "Build a half wall")){
    39.             //preview = Instantiate (HalfWall, gameObject.transform.position + gameObject.transform.forward + ((gameObject.transform.up * -1) * 2), gameObject.transform.rotation) as Transform;
    40.             while(Placed == false && Gold >= 100){
    41.                     //preview.transform.position = (gameObject.transform.position + (gameObject.transform.forward  * 7) + ((gameObject.transform.up * -1) * 2));
    42.                     if(Placed == false){
    43.                         Network.Instantiate (HalfWall, gameObject.transform.position + (gameObject.transform.forward * 7) + ((gameObject.transform.up * -1) * 2), gameObject.transform.rotation, 0);
    44.                         Placed = true;
    45.                         Gold = Gold - 100;
    46.                     }
    47.                 }
    48.             }
    49.             if(Gold < 100){
    50.                 Debug.Log ("You don't have enough gold!");
    51.             }
    52.         Placed = false;
    53.     }
    54.  
    55.     void OnCollisionStay(Collision coll){
    56.         print ("Got here!");
    57.         if(coll.gameObject.tag == "Helicopter" && Input.GetKeyDown (KeyCode.F) && InVehicle == false){
    58.             InVehicle = true;
    59.             Vehicle = coll.gameObject;
    60.             Vehicle.GetComponent ("Helicopter").SendMessage ("PlayerGettingIn");
    61.             Camera.active = false;
    62.             Physics.IgnoreCollision (gameObject.collider, coll.collider, true);
    63.             print ("Got to the OnCollisionStay function!");
    64.         }
    65.         if(InVehicle && Input.GetKeyDown (KeyCode.F)){
    66.             InVehicle = false;
    67.             Camera.active = true;
    68.             Vehicle = coll.gameObject;
    69.             Vehicle.GetComponent ("Helicopter").SendMessage ("PlayerGettingOut");
    70.             Physics.IgnoreCollision (gameObject.collider, coll.collider, false);
    71.         }
    72.     }
    73. }
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Both need to have a rigidbody if they don't already.
     
  3. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    They both have rigidbodies.
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    I see what's going on here :p

    You are saying

    InVehicle = true

    Then saying if InVehicle do something (in this case, get out instantly). You should swap the order of these if statements