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.
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.
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")) {
// ...
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
}
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.