- Home /
Car Enter/Exit Script Multiple Cars Problem
Hello, im quite new to unity answers and i was just hoping that someone could help me with my problem, ive been racking my brain for ages and i just cant figure it out
basicly my enter exit script works 100% fine whenever i have one single car in my scene but once i add a second (by copy and pasting the first) the second car wont disable the player but the player will still be attached to the car and the car will be controllable however the other car will always work perfectly fine. another thing i noticed is that while driving the second car i cant diable the player through the inspectors check box as it immediately becomes rechecked instantly
here is my enter/exit script (its in js):
var inTrigger = false;
var incar = false;
var player : Transform;
var exitPoint : Transform;
var Car : Transform;
function OnTriggerEnter(collider : Collider)
{
if(collider.tag == "Player")
{
inTrigger = true;
}}
function OnTriggerExit(collider : Collider)
{
if(collider.tag == "Player")
{
inTrigger = false;
}}
function Update(){
if(inTrigger){
if (Input.GetKeyDown ("e"))
incar = (!incar);
}
if(inTrigger == false){
incar = false;
}
if(incar){
player.gameObject.SetActive (false);
// Parent player to ExitPoint
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(-0.25,0,0);
//Parent playerParent to car
exitPoint.parent = Car.transform;
exitPoint.transform.localPosition = Vector3(-0.25,0,0);
// Enable car as controllable object
Car.GetComponent("DrivingScript").enabled = true;
}
else{
player.gameObject.SetActive (true);
// Unparent Player from everything.
player.transform.parent = null;
// Disable car as a controllable
Car.GetComponent("DrivingScript").enabled = false;
}
}
Anyone please? I have no clue what to do. Even if your not completely sure about it just somebody to help elibrate with would be amazing
Did you know about OnTriggerStay() fonction? You should give it a try! I think there is conflict your some vars.
OnTriggerStay is executed every frame the player is in the collider. So you could change OnTriggerEnter function for OnTriggerStay() and You could put your input.getkey in this function and probably get rid of the inTrigger bool...
I'll look further...
thank you soo much for commenting you have no idea how much this means to me. i really apreciate your help, and thanks for the tip with OnTriggerStay()
@mnm53 Could I get a little more information about this script? I'm very interested in it, but I don't know too much about scripting, so I'm not sure how to make it work in my scene. A few questions:
What script language is it written in?
What game object(s) do I add the script to (is it just a universal script to be added to a single empty object, or to be added to every vehicle, etc)?
Is that the full script, or is it missing the opening lines (such as "using UnityEngine;" etc)?
Is the script compatible with Unity 5, or does it need to be updated?
Does the name of the script matter, or can I name it anything without encountering a compiling error?
The reference to another script, "DrivingScript", is that just a placeholder name that can be replaced with any script that controls the vehicle in question, such as the Standard Assets' "Car Controller" script?
Sorry for all the questions, but when it comes to scripting, I need all the help I can get. Thanks.
Answer by mnm53 · Dec 10, 2014 at 09:12 AM
i actually managed to figure it out and it really still confuses me but i decided to test someone else's Enter/Exit Script and i found the problem to be with this line: incar = (!incar);
and the problem stops if you include a else function like i have done in the completed fully functional script below (btw the script below is actually another script ive found on unity answers and ive modified it if a few ways, it still follows roughly the same principle though)
var car : Transform;
var player : Transform;
var exitPoint : Transform;
var doorTriggerLeft : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera;
var isPlayerVisible : boolean;
var driving : boolean;
function Update()
{
if(driving){
isPlayerVisible = true;
}
if (Input.GetKeyDown("f")&& isPlayerVisible)
{
driving = !driving;
if(driving){
player.gameObject.active = false;
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(0,0,0);
exitPoint.parent = car.transform;
exitPoint.transform.localPosition = Vector3(-0.5,0,0);
car.GetComponent("DrivingScript").enabled=true;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
}
else
{
player.gameObject.active = true;
player.transform.parent = null;
exitPoint.parent = car.transform;
car.GetComponent("DrivingScript").enabled=false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
}
}
}
function OnTriggerEnter(Player : Collider)
{
isPlayerVisible = true;
}
function OnTriggerExit(Player : Collider)
{
isPlayerVisible = false;
}
Answer by Delta6 · May 21, 2015 at 09:16 AM
yah I have also been looking for a script that will work in ta tank and a helicopter and have found little hope. but this script seems very promising.
Answer by dooly123 · Oct 17, 2016 at 07:25 AM
hay all been working on a script for a few months now its finished sadly its not free but if you are not able to get ur own one to work i thought it might help you get back on track :) https://forum.unity3d.com/threads/enter-exit-vehicle-networked-photon-cars-planes-offline-online-easy-to-use.436698/
Your answer
Follow this Question
Related Questions
Enter/Exit vehicle 0 Answers
Problem Enter and Exit with Vehicle 2 Answers
How do you make character enter and exit vehicles. 1 Answer
Enter\Exit Car With Doors. 0 Answers
could someone help with entering/exiting a vehicle? 0 Answers