- Home /
How do I mount a Vehicle?
My Problem is, I'm trying to mount a vehicle by deactivating the Player and his camera (which is a child of the player) and activating the car's Drive Script and it's camera using a "MountVehicle" Script on the Car. If I want to Exit the car, I dectivate the Drive Script and the Camera, activate the Player and pull the Player to the ExitPoint (a child of the Car).
The Code looks like this:
var Cam : GameObject;
var Player : GameObject;
var driving : boolean = false;
var DriveScript : Component;
var ExitPoint : Transform;
function Start () {
}
function Update () {
if(driving==true && Input.GetButtonDown("E")){
Cam.active = false;
DriveScript.active = false;
Player.transform.position = ExitPoint.position;
Player.active = true;
driving = false;
}
}
function OnTriggerStay () {
if(Input.GetButtonDown("E")){
//activate Camera and Drive Script
Cam.active = true;
DriveScript.active = true;
//deactivating Player
Player.active = false;
yield WaitForSeconds(1);
driving = true;
}
}
But if I deactivate the Player GameObject, I dont deactivate it's Children and it looks like the Player is standing next to the car, while he is driving it.
I also cant activate the Drive Script with this method.
Answer by fafase · Aug 08, 2012 at 10:24 AM
Add a trigger around the car:
var script:CarScript;
function Start(){
script= gameObject.getComponent(CarScript);
script.enabled=false;}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Player")script.enabled = true;}
function OnTriggerExit(other:Collider){
if(other.gameObject.tag=="Player")script.enabled = false;}
on the car script:
var insideCar : boolean;
var player :GameObject;
function Start(){
player =GameObject.Find("Player");
insideCar = false;}
function Update(){
if(Input.GetKeyDown(KeyCode.E)){
if(!insideCar){ // if not in the car
player.transform.parent = transform; // the player is attached to the car
player.SetActiveRecursively(false); // disable object and children of player
Camera.main.transform.position = Vector3(camPositionBehindCar);// position the camera behind the car
Camera.main.transform.parent = transform; //Attach the cam to the car
}else{ // the player is inside we want to get out
player.transform.parent = null; // the player is free to move
player.SetActiveRecursively(true); //the player and its children are activated
Camera.main.transform.position = Vector3(camPositionBehindPlayer);// camera is positioned behind the player
Camera.main.transform.parent = player.transform; //camera is attached to the player
}
}
I do not have Unity so it probably needs to be tried and fixed. But hopefully you get the idea.
Edit: I added the part for getting out. Still need to be confirmed.
Answer by DasFloX · Aug 08, 2012 at 04:53 PM
Oh Thanks, that is very helpful.
But unfortunately I still cant drive without activating the Drive Script somehow.
$$anonymous$$aybe you should put the script on the car. You could have the car script disabled so that it does not take any resources and a trigger around the car that enables the car script on enter and invert on exit.
From the car script you can control input and then disable your guy, attach the camera and so on.
Answer by DasFloX · Aug 08, 2012 at 05:45 PM
I found the Answer,
var DriveScript : MonoBehavior;
and then:
DriveScript.enabled = false;
Answer by DasFloX · Aug 08, 2012 at 05:45 PM
How do I invert the effect of attaching the player to the car?
This should be a comment not an answer. That would avoid going through moderation.
No pb. I was new once. You could delete that "answers" and post your comments below the answer you are refering to. Fast and efficient
Your answer
Follow this Question
Related Questions
How to disable children triggering the collision event of parent. 1 Answer
Enable/Disable children / group of objects 0 Answers
is it possible to destroy the player and instantiate another controller? 1 Answer
Disabling audio Listener on the child of a newly instantiated gameObject? 0 Answers
Getting an objects Children 1 Answer