Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EnterBoat : MonoBehaviour
- {
- [Header("In-Boat Positions")]
- public float xValue, yValue, zValue;
- [Header("Exit-Boat Positions")]
- public float Xvalue, Yvalue, Zvalue;
- private bool inBoat = false;
- BoatDrive vehicleScript;
- public GameObject Node;
- public GameObject player;
- public GameObject cameraBoat;
- void Start()
- {
- player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().enabled = true;
- player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = true;
- vehicleScript = GetComponent<BoatDrive>(); // You want to make sure that the vehicleScript equals the CarUserControl Script of our car.
- player = GameObject.FindWithTag("Player"); // We want to make sure our player is in the scene, otherwise this script won't work.
- vehicleScript.enabled = false; // Once our vehicleScript has been found, we want to disable it for now, as we enable it later on.
- cameraBoat.SetActive(false); // We want to disable the car camera and use our player camera.
- // guiObj.SetActive(false); Text with "Press E" for example
- }
- void OnTriggerStay(Collider other) // When being into the ColliderBox with Trigger Checked
- {
- if (other.gameObject.tag == "Player" && inBoat == false)
- {
- if (Input.GetKey(KeyCode.H))
- {
- player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
- player.GetComponent<Rigidbody>().freezeRotation = true;
- player.GetComponent<Rigidbody>().isKinematic = true;
- player.GetComponent<Animator>().enabled = false;
- player.GetComponent<Animator>().Play("Idle");
- player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().enabled = false;
- player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = false;
- player.transform.parent = Node.transform; // We have set the player as a parent of the Car. We here switch to the Car.
- player.transform.localPosition = new Vector3(xValue, yValue, zValue); // We are setting the players position to the assigned values for x, y, z
- vehicleScript.enabled = true; // We want to enable our vehicleScript here, so we can drive.
- 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.
- inBoat = true; // We want to say that we are in the car.
- cameraBoat.SetActive(true); // We want to change the current camera to the car camera.
- }
- }
- }
- void Update()
- {
- 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.
- {
- player.transform.localPosition = new Vector3(Xvalue, Yvalue, Zvalue);
- player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
- player.GetComponent<Rigidbody>().freezeRotation = true;
- player.GetComponent<Rigidbody>().isKinematic = false;
- vehicleScript.enabled = false;
- player.SetActive(true);
- player.GetComponent<Animator>().enabled = true;
- player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl>().enabled = true;
- player.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = true;
- player.transform.parent = null;
- inBoat = false;
- cameraBoat.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement