- Home /
Crafting collision Trigger not working
I apologize for the confusing title, but i seem to be having a problem with my code. The way its supposed to work ism when i enter the collider and press e its supposed to open up a crafting menu UI which it does not. I've checked whether or not its detecting the collisions but that dosent seem to be the problem. If any of you guys could perhaps tell me how to fix this issue i would be grateful. Also if you have any suggestions on how you guys want me to make the crafting system it would be also very much appreciated. Thanks!
The code: #pragma strict
var CraftMenu : GameObject;
var UIcanvas : GameObject;
var ToggleWorkbench : int = 10;
function Start () {
ToggleWorkbench = 1;
}
function Update () {
}
function OnTriggerStay (col : Collider){
if(Input.GetKey(KeyCode.E)){
switch(ToggleWorkbench){
case 0:
ToggleWorkbench = 1;
Time.timeScale = 0;
UIcanvas.active = false;
CraftMenu.active = true;
break;
case 1:
ToggleWorkbench = 2;
Time.timeScale = 1;
UIcanvas.active = true;
CraftMenu.active = false;
break;
}
}
}
Answer by sandeepsmartest · Dec 08, 2015 at 06:45 AM
There are several ways to achieve this. Best way would be to check if the object is inside collider or not .For this make a identifier that becomes true when the object enters into collider you can get this by simply using OnTriggerStay. As soon as if player comes out of trigger zone make that identifier immediately false by simple using OnTriggerExit. Under update check if button is pressed and if identifier is true. By using these OnTriggerEnter,OnTriggerStay and OnTriggerExit you can check the collision state of the player Below given is very basic untested code written in c# this may be helpful you.
public bool IsPlayerInTriggerZone;
void Update()
{
if(IsPlayerInTriggerZone && Input.GetKey(KeyCode.C))
{
//do your stuff
}
}
void OnTriggerEnter(Collider Col)
{
if(Col.gameObject.tag="")
{
print("Player just entered Trigger Zone");
IsPlayerInTriggerZone = true;
}
}
void OnTriggerStay(Collider Col)//Player Staying in trigger zone
{
if(Col.gameObject.tag=="TriggerZone")
{
print("Player Staying in trigger zone");
IsPlayerInTriggerZone = true;
}
else
{
print("Player Staying NOT in trigger zone");
IsPlayerInTriggerZone = true;
}
}
void OnTriggerExit(Collider col2)//plyer just came out from trigger zone
{
print("plyer just came out from trigger zone");
IsPlayerInTriggerZone = false;
}
NSKS
Your answer
Follow this Question
Related Questions
Collision of a constantly moving object 0 Answers
Ignore Character Controller Collision 1 Answer
How to create a semi-passable barrier 1 Answer
NavMesh Collision Detection? 0 Answers