I’m trying to make a game with a first person that can enter a car. I’m using this script:
var car : Transform;
var player : Transform;
var exitPoint : Transform;
var doorTriggerLeft : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera;
var isPlayerVisible : boolean;
function Update(){
if (Input.GetButtonUp("Action") isPlayerVisible){
// make player invisible and still standing
player.gameObject.SetActiveRecursively(false);
player.gameObject.active = false;
// parent player to Exit Point
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(-1.5,0,0);
// parent PlayerParent to car
exitPoint.parent = car.transform;
exitPoint.transform.localPosition = Vector3(-0.5,0,0);
// Enable Car as controllabe object
GameObject.Find("car").GetComponent("DrivingScript").enabled=true;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
}
else
{
if (Input.GetKeyUp("r")){
// make character visible again
player.gameObject.SetActiveRecursively(true);
player.gameObject.active = true;
// unparent player from everything
player.transform.parent = null;
// parent Exit Point to door Trigger
exitPoint.parent = doorTriggerLeft.transform;
// disable car as controllable
GameObject.Find("car").GetComponent("DrivingScript").enabled=false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
}
}
}
function OnTriggerEnter(Player : Collider) {
isPlayerVisible = true;
}
function OnTriggerExit(Player : Collider) {
isPlayerVisible = false;
}
I’ve assigned all the options to the transformers and cameras.
Ok , so there’s two problems when I assign “E” to my action button in inputs , the “E” just seems to disappear , any way to stop this?
Secondly, is there an easier script I could use to make a vehicle which I could enter and exit?
this is a script i have made, using some documentation. I am a beginer too, but i hope it will help :
//Enter Exit Car Script By Udiah Nila - [email]hmlanila@yahoo.com[/email]
var Car : Transform;
var Player : Transform;
var DoorTrigger : Transform;
var ExitPoint : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera;
private var IsPlayerInTrigger : boolean;
private var IsPlayerInCar : boolean = false;
var BackWheelLeft : WheelCollider;
var BackWheelRight : WheelCollider;
//This two functions will sense when the player is in the door trigger area
//Aceste doua functii vor simti cand playerul este in zona masinii
function OnTriggerEnter(Player : Collider) {
IsPlayerInTrigger = true;
print("PlayerEnteredTrigger");
}
function OnTriggerExit(Player : Collider) {
IsPlayerInTrigger = false;
print("PlayerExitedTrigger");
}
function Update () {
// Here will tell them what to do
// Aici le spunem ce sa faca
// If we press the desired key and the player is in the trigger area
// Daca apasam tasta dorita si playerul este in zona masnii
if (Input.GetKeyDown("f")){
if (IsPlayerInTrigger !IsPlayerInCar) {
print("Player Is In Trigger, Will Enter VEHICLE");
// We will DEACTIVATE, atention, not DISABLE the player
// Vom DEZACTIVA, atentie, DEZACTIVA, nu DEZABILITA playerul
Player.parent = ExitPoint.transform;
Player.transform.localPosition = Vector3(0,0,0);
Player.transform.localRotation = Quaternion.identity;
Player.active = false;
PlayerCamera.GetComponent(AudioListener).enabled = false;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
Car.GetComponent(AlinCAR).enabled = true;
Car.GetComponent(AudioSource).enabled = true;
CarCamera.GetComponent(AudioListener).enabled = true;
IsPlayerInCarTrue();
BackWheelLeft.brakeTorque = 0;
BackWheelRight.brakeTorque = 0;
IsPlayerInTrigger = false;
}
if (IsPlayerInCar) {
print("Player Will Exit Car NOW!");
BackWheelLeft.brakeTorque = 10000;
BackWheelRight.brakeTorque = 10000;
Player.active = true;
PlayerCamera.GetComponent(AudioListener).enabled = true;
Player.transform.parent = null;
Player.transform.localRotation = Quaternion.identity;
Car.GetComponent(AlinCAR).enabled = false;
Car.GetComponent(AudioSource).enabled = false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
CarCamera.GetComponent(AudioListener).enabled = false;
IsPlayerInCarFalse();
}
}
}
function IsPlayerInCarFalse() {
yield WaitForSeconds(0.5);
IsPlayerInCar = false;
}
function IsPlayerInCarTrue() {
yield WaitForSeconds(0.5);
IsPlayerInCar = true;
}
You will just have to set the script up and it will work nice.