MY SCRIPT WILL BE AVAILABLE ON THE ASSET STORE FOR $15 VERY SOON!!
A GTA STYLE GAME WOULDNT B POSSIBLE WITHOUT IT!!!
WHAT IT CAN DO:
You can Simply run up to your car, press a key ("e" in my case) and enter your car.
at this point your character is invisble and you can drive your car around while your invisible character gets steady moved with it.
after pressing another key - (in my case "r") your character gets visible again and stands right besides your car. and you are now able to controll your character again while your car still stands where u left it. :)
Here its me again with general answer. I won't code the whole thing, but try something along these lines and come back with your problems. If you don't need anything fancy, here's how you could order things:
Player enter Car trigger => Player learn of car existence
Player hit a key and a car is known => function enterCar
Play animation?
Destroy Player / Disable Renderer / Hide under terrain
Give control to car > Enable Script / Set a boolean variable
Move around
Player hit a key = > function exitCar
Take control off car
Play animation?
Instantiate Player back at the left of the car
Give control back to player
Can't believe I wrote all this, scripting would've been just as long but then I'd have done all the work ;)
DUPLICATE AND CALL THE FOLLOWING SCRIPT “VehicleDoorLeft” & “VehicleDoorLeft”
WHAT IT CAN DO
You can Simply run up to your car, press a key (“e” in my case) and enter your car.
at this point your character is invisble and you can drive your car around while your invisible character gets steady moved with it.
after pressing another key - (in my case “r”) your character gets visible again and stands right besides your car. and you are now able to controll your character again while your car still stands where u left it.
KNOWN ISSUES:
YOU CAN ENTER THE VEHICLE FROM EVERYWHERE YOU ARE?
SOLUTION: **DISABLE** the "*isPlayerVisible*" box by Default before you start the game.
Your problem will be solved.
Do NOT set another gameObject with a Collider Attached to it as a child of your Player Character.
Whether you deactivate the collider or set it as “is Trigger”.
Otherwise your Car will behave VERY VERY STRANGE and you dont want this to be happen
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 created a video tutorial for the script’s implementation. I didn’t tag the ‘car’ to vehicle, tag it according to ‘Yung Hustla’. This script works really good, good work bro!
Hey my script says Assets/_Scripts/EnterExit.js(24,71): BCE0019: ‘enabled’ is not a member of ‘UnityEngine.Component’.
please help since I am only a modeler