- Home /
How to have controlles switch from FPC to a RC Car
Hello Unity Community! I have been having the greatest time in Unity3d but have come across a problem. I am trying to have were the player, which would be a First Person Controller comes to a control stand (3d model)is able to press a key, say the button D button on the key board. This function sends the controlles and camera to a RC car witch the player can operate. Once the person is done using the RC car he is able to exit out or when he dies he is returned to his original body. I would complete this objective by myself but can not script worth a darn. Any scripts or references to a website with the required programming would be fantastic. Thank You so much Sincerely, Cobalt 60
Answer by ByteSheep · Dec 07, 2011 at 02:54 AM
If you have a script to control your player and a script to control your RC car (I'm assuming you have already created individual controls for both), then you can use a variable to enable or disable the control scripts: GetComponent("ScriptName").enabled = false;
With this you can dis/en-able your scripts.. It could look something like this:
var PlayerGameObject : GameObject;
var CarGameObject : GameObject;
var Player : Transform;
var Car : Transform;
var PlayerDied : boolean = false;
function Update () {
if(Input.GetButtonUp("D"))
{
if(Player.GetComponent("ScriptName").enabled == true && Car.GetComponent("test").enabled == false)
{
Player.GetComponent("test").enabled = false;
Car.GetComponent("test").enabled = true;
PlayerGameObject.camera.enabled = false;
CarGameObject.camera.enabled = true;
}
if(Player.GetComponent("ScriptName").enabled == false && Car.GetComponent("test").enabled == true || PlayerDied == true)
{
Player.GetComponent("test").enabled = true;
Car.GetComponent("test").enabled = false;
PlayerGameObject.camera.enabled = true;
CarGameObject.camera.enabled = false;
}
}
}
You will have to assign all of the variables (GameObjects and Transforms) in the inspector view and you may have to define the Input key "D" under Edit>Project Settings>Input . You will also have to set PlayerDied to true when your player dies, hope this helps!
And forgot to mention the "ScriptName" and "test" must be replaced with the names of your scripts
So sorry to ask this but is it possible to put a range where you can get in the car when you are 5 feet away or so. thanks for everything you have done so far.
Yes that should be simple enough, add this script to the car object:
var player : Transform; var dist : float; static var InRange : boolean; var $$anonymous$$Distance : float = 5;
function Update () {
dist = Vector3.Distance(player.transform.position, transform.position);
if(dist
}
Then name this script "CarDistance" and find the line: if(Input.GetButtonUp("D")) in the first script.
Turn that line into:
if(Input.GetButtonUp("D") && CarDistance.InRange == true)
This script is calculating the distance between the players transform and the cars transform, getting a float value that (when lower than $$anonymous$$Distance) will set InRange to true. The other script is checking whether InRange is true and will therefor only allow the player to enter the vehicle if close enough ;)
Thank you so much I don't think I will ever be able to repay you and I am sorry for adding and adding to the script. I was wondering if it was possible that when the person uses the rc car or vehicle it takes the player with the vehicle and when the player decides to get out it drops him off where the vehicle had stoped. Thank you so much for everything.
Your answer
Follow this Question
Related Questions
Camera not Following Inspector's Value (Transform) When Play 0 Answers
Toggle Control between multiple turrets 0 Answers
Viewer position lost in Space. How to get back to scene? 1 Answer
How do I calculate a view matrix using Matrix4x4.LookAt? 2 Answers
How to change first person controller input when passing through a collider? 1 Answer