- Home /
i cant get an object to become child of an other JS
im trying to make a script to get the player to get on a vehicle, for some reason it doesent parent it to the vehicle. here is the code im using:
#pragma strict
var changePosition = boolean;
var enterVehicle : boolean = false;//this var is to now if he has allready entered the vehicle
var canEnter : boolean = false;//this one is to know if he is allowd or not to enter
var seat : Transform;
var vehicleTransform : Transform;
@HideInInspector
var enterText : GUIText;// this one is the component and the one bellow is the object that holds it
var textObject : GUIText;
@HideInInspector
var isInside : boolean = false;
function Start ()
{
enterText = textObject.GetComponent(GUIText);//here i set sest the componet to the variable
enterText.enabled = false;
}
function Update ()
{
if(isInside && !enterVehicle)
enterText.enabled = true;
else if(!isInside && !enterVehicle)
enterText.enabled = false;
}
function OnTriggerEnter (hit : Collider)
{
if (hit.transform.name == "Player" && !isInside)
{
if (Input.GetKey(KeyCode.E)){
print("IM IN!!!");
hit.transform.position = seat.transform.position;
hit.transform.parent = vehicleTransform;
}
vehicleTransform.SendMessage("Interact", SendMessageOptions.DontRequireReceiver);
isInside = true;
}
}
function OnTriggerExit (hit : Collider)
{
if (hit.transform.name == "Player" && isInside)
{
isInside = false;
}
}
i have tried averything and i realy dont know what could be wrong.
Answer by Piflik · Jan 01, 2013 at 08:33 PM
Don't test for the Input in OnTriggerEnter. OnTriggerEnter is only called in one frame and it is highly unlikely that you press the button on the same frame. Instead set a boolean value to true on trigger enter and test against it and the Input in Update...
private var closeToVehicle : boolean = false;
function Update() {
if(Input.GetKeyDown(KeyCode.E) && closeToVehicle) {
GetInVehicle();
}
}
function OnTriggerEnter(hit : Collider) {
if(hit.gameObject.tag == "Player")
closeToVehicle = true;
}
function OnTriggerExit(hit : Collider) {
if(hit.gameObject.tag == "Player")
closeToVehicle = false;
}
function GetInVehicle() {
//code here
}
Warning: Has been some time since I wrote UnityScript...don't jsut copy this into your code and expect it to work bugfree ;)
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Javascript Detecting a prefab's child's collision 2 Answers
Line up camera views 0 Answers
How to detect child object collisions on parent 3 Answers
Collision with child 0 Answers