Advertisement
Guest User

EnterBoat Script

a guest
Feb 17th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. public class EnterBoat : MonoBehaviour
  2. {
  3.     [Header("In-Boat Positions")]
  4.     public float xValue, yValue, zValue;
  5.  
  6.     [Header("Exit-Boat Positions")]
  7.     public float Xvalue, Yvalue, Zvalue;
  8.  
  9.     private bool inBoat = false;
  10.     BoatDrive vehicleScript;
  11.     public GameObject Node;
  12.     public GameObject player;
  13.     public GameObject cameraBoat;
  14.  
  15.     void Start()
  16.     {
  17.         player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().enabled = true;
  18.         player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = true;
  19.         vehicleScript = GetComponent<BoatDrive>(); // You want to make sure that the vehicleScript equals the CarUserControl Script of our car.
  20.         player = GameObject.FindWithTag("Player"); // We want to make sure our player is in the scene, otherwise this script won't work.
  21.         vehicleScript.enabled = false; // Once our vehicleScript has been found, we want to disable it for now, as we enable it later on.
  22.         cameraBoat.SetActive(false); // We want to disable the car camera and use our player camera.
  23.         // guiObj.SetActive(false); Text with "Press E" for example
  24.     }
  25.  
  26.     void OnTriggerStay(Collider other) // When being into the ColliderBox with Trigger Checked
  27.     {
  28.         if (other.gameObject.tag == "Player" && inBoat == false)
  29.         {
  30.             if (Input.GetKey(KeyCode.H))
  31.             {
  32.                 player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
  33.                 player.GetComponent<Rigidbody>().freezeRotation = true;
  34.                 player.GetComponent<Rigidbody>().isKinematic = true;
  35.                 player.GetComponent<Animator>().enabled = false;
  36.                 player.GetComponent<Animator>().Play("Idle");
  37.                 player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().enabled = false;
  38.                 player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = false;
  39.                 player.transform.parent = Node.transform; // We have set the player as a parent of the Car. We here switch to the Car.
  40.                 player.transform.localPosition = new Vector3(xValue, yValue, zValue); // We are setting the players position to the assigned values for x, y, z
  41.                 vehicleScript.enabled = true; // We want to enable our vehicleScript here, so we can drive.
  42.                 player.SetActive(true); // We want to disable our player, so we can switch to the car. Otherwise we will see the player while we drive.
  43.                 inBoat = true; // We want to say that we are in the car.
  44.                 cameraBoat.SetActive(true); // We want to change the current camera to the car camera.
  45.             }
  46.         }
  47.     }
  48.  
  49.     void Update()
  50.     {
  51.         if (inBoat == true && Input.GetKey(KeyCode.X)) // If we are in the car and we press "X", we want to get out of the car and set everything back.
  52.         {
  53.             player.transform.localPosition = new Vector3(Xvalue, Yvalue, Zvalue);
  54.             player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
  55.             player.GetComponent<Rigidbody>().freezeRotation = true;
  56.             player.GetComponent<Rigidbody>().isKinematic = false;
  57.             vehicleScript.enabled = false;
  58.             player.SetActive(true);
  59.             player.GetComponent<Animator>().enabled = true;
  60.             player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().enabled = true;
  61.             player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = true;
  62.             player.transform.parent = null;
  63.             inBoat = false;
  64.             cameraBoat.SetActive(false);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement