- Home /
GetKeyUp inside a collider only? How?
My question is a really simple one but at this stage it has really got me stuck! How can I add the OnTriggerEnter to my script so that it only activates the GetKeyUp when inside a collider?
I have two controllable characters (for simplicity I will call them Player A & Player B). Player B has a rigidbody applied, a small cube parented to it with a box collider and parenting script (for triggering when Player A is close enough to get parented to Player B). Further I have the below script attached to both players which allows me to change controls Player A ---> Player B using GetKeyUp "2", Player B ---> Player A using GetKeyUp "1" (if Player A is inside the small cube when pressing "2", since they were parented to Player B, they will now move alongside Player B after the controller switch). Here is the script I am using:
// add your player and camera
var PlayerA : GameObject;
var PlayerB : GameObject;
var PlayerACam : GameObject;
var PlayerBCam : GameObject;
function Start (){
PlayerA.active = true;
PlayerB.active = false;
PlayerACam.camera.enabled = true;
PlayerBCam.camera.enabled = false;
}
function Update (){
// Hit 1 key to PlayerA playable
if(Input.GetKeyUp("1")){
PlayerA.active = true;
PlayerB.active = false;
PlayerACam.camera.enabled = true;
PlayerBCam.camera.enabled = false;
}
// Hit 2 key to PlayerB playable
if(Input.GetKeyUp("2")){
PlayerB.active = true;
PlayerA.active = false;
PlayerBCam.camera.enabled = true;
PlayerACam.camera.enabled = false;
}
}
Both players have this script attached to them since when one gets disabled, the other needs to be able to re-enable them. My understanding is that only Player B would need to have the OnTriggerEnter function added since it will be the one with an attached trigger collider. I am really stuck though as to how to implement this into the script :(
So, for clarity my question here again: How can I add the OnTriggerEnter to my script so that it only activates the GetKeyUp when Player A is inside Player B's collider?
Any help at this point would be greatly appreciated. Thank you, and looking forward to a reply.
FYI: try to have titles which are clear and specific about your problem
I have found the following links here and here, but both of the questions are unresolved. I have tried integrating the scripts but I always get a number of errors. $$anonymous$$aybe the answers are outdated. Any progress from your side? I can't believe there isn't a single answer here to something so simple and quite commonly used :(
Answer by gfr · Oct 20, 2011 at 10:55 AM
One simple approach would be setting a flag:
var insideZone : boolean = false;
function OnTriggerEnter (other : Collider) { if (/ check for tag or something /) { insideZone = true; } }
function OnTriggerExit (other : Collider) { if (/ check for tag or something /) { insideZone = false; } }
function Update() { if(insideZone && Input.GetKeyUp("1")) { // ...
I'm sure you meant OnTriggerExit for that second function.
Could the problem be from the fact that you are using OnTriggerEnter (which calls the function only during the frame when the player enters the collider) as oppose to OnTriggerStay or OnCollisionStay which would call the function during the entire time that the player is inside the vehicle trigger box? Allowing the player the ability to press the "2" key as long as the collision was occurring.
It worked! Thank you @Hamesh81 for providing the link and @ScriptGirl for piecing the ideas together, and of course a big thank you to @gfr for his huge help also.
When pressing 2 outside the collider it does nothing. When entering the collider and pressing 2 it changes controls. There remains a problem however with the final function. For some reason the debug.log keeps returning "2 was pressed" whether you are inside or outside the collider. Shouldn't it return this only when both conditions are met?
Debug.Log is outside the if: move it right after PlayerACam.camera... and it will work as expected.
Glad you guys managed to get it working!! Gd to see you kept at it despite what others said :)
Btw, I'm not expecting any credit since I just gave you guys a few links, but I'm glad they helped. I think Pilot and gfr did most of the work anyway. Cheers
Answer by aldonaletto · Oct 20, 2011 at 03:28 PM
The whole idea has a problem: if you set PlayerB.active to false, it disappears and its trigger doesn't work anymore. If you want PlayerB to disappear, replace it with an invisible cube to serve as the trigger. If you just want PlayerB to stay static and doesn't respond to controls while PlayerA is active, it's better to disable PlayerB scripts instead. Anyway, using different scripts in each player is better than using the same script, since both have different behaviours.
When PlayerB.active is set to false the trigger DOES still work. Since when they aren't active and PlayerA enters the trigger they are automatically parented to PlayerB via a script attached to the trigger. Using my original script, when key 2 is pressed the controls can then be switched and PlayerB will be able to move with PlayerA parented to them.
I don't understand why I would want to make a vehicle disappear or be invisible? And as mentioned in the original question, each player GO has a separate script attached to them which is how one can turn off and enable the other.
@aldonaletto: Are you sure you read the question, because it seems you have misunderstood what ScriptGirl & Pilot are trying to achieve here.
@ScriptGirl & @Pilot: I have managed to find the following link which you might find useful. It's a bit outdated but uses OnTriggerStay in a similar way to what you are trying to do, you might find it helpful: here is the link.
@ScriptGirl, setting a game object active to false deactivates all of its components, including collider and renderer, what deactivates trigger detection and makes the object invisible. Ok, I understand now that the trigger volume is a parent (or child?) of it, but still can't understand how the deactivated object will stay visible.
But never $$anonymous$$d: if this approach is working for you, go ahead!
Answer by Pilot · Oct 21, 2011 at 12:03 PM
The original script needs to be combined with the script provided by gfr. The collider in question needs to be tagged to differentiate it from the other gameobjects. Accordingly, in the if statement of the script, the same tag needs to be called in order to check whether in fact it is the player that is colliding with the relevant collider. Use the links posted by Hamesh81 and myself if you are having trouble writing this part of the script. The update function only needs to contain the if statement for switching to the other gameobject (since you don't need to switch to the gameobject which is already active).
Use the debug.log as gfr suggested in order to test specific parts of the script. Also note that while the script used here is quite basic, there are many situations where things can go wrong, so I would not recommend trying this if you are a complete beginner. However with care and patience even beginners should be able to pull this off. Hope this answer is helpful to anyone trying to do similar things to this.
Answer by DavidDebnar · Oct 20, 2011 at 05:38 PM
So when I understand it, what you wanna do is make a player go into a vehicle. You could either Raycast, check for distance or check for collision. Ill make a simple version of your script.
var player : Transform; //sorry but I use transforms more often and I'm used to em :).
var vehicle : Transform;
function Start()
{
//disable the vehicle here, but not the whole thing. Disable just the scripts and controllers on it, don't disable the collider/renderer (I suspect, that you wanna have your vehicle visible)
}
function Update()
{
//I will script all 3 versions
Ver1();
/*Ver2();*/
}
function Ver1()
{
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 1)) //check if something is 1 meter forward
if(hit.collider.tag == "vehicle" && Input.GetKeyDown(KeyCode.1))
HopIn();
}
function Ver2()
{
if(Vector3.Distance(player.position,vehicle.position) <= 1 && Input.GetKeyDown(KeyCode.1))
HopIn();
}
/*function OnTriggerEnter(hit : collider) //Ver3
{
if(hit.collider.tag == "vehicle" && Input.GetKeyDown(KeyCode.1))
HopIn();
}
*/
function HopIn()
{
//make the vehicle enabled here, and the player disabled/parented to it
}
David