im trying to make it so that when you are looking at the torch you can pick it up however when i press E any where in the level the torch is picked up, its the same button to open doors but if i press E anywhere it doesnt open the doors it only picked up the torch.
You are setting CanGetTorch to true as soon as you first look at it, and never setting it to false again after that, which causes you to pick it up even if you’re not looking at it after that. This also causes you to be able to pick it up anywhere on the scene after looking at it once.
I don’t see any code for actually pressing a button to open doors even with the DoorCanOpen set to true, so can’t comment on that.
In case you can pick the torch up even without looking at it, I’d also check the torch collider settings to make sure there are no colliders that are excessively large etc.
Also a tiny performance improving suggestion: I would also, instead of calling raycast every single update for simple things like activating a single item, prefer to call the raycast when pressing the activation button and checking for whatever is in the way, then calling the activation method on that item (be it a door or torch or anything else).
Rather then using Input class you can simply use OnMouseOver() to check if the pointer is over the gameObject.
Then check if the gameObject is the one that you wanna pick and pick it.
void Awake () {
// PlayerCasting uses Physics.Raycast to and updates distanceFromTarget float value.
distance = PlayerCasting.distanceFromTarget;
}
void Update () {
distance = PlayerCasting.distanceFromTarget;
if (!tourchPicked && Input.GetButtonDown("Action"))
{
if (distanceValue)
{
StartCoroutine("PickTourch");
}
}
}
IEnumerator PickTourch() {
// Write the code to Pick tourch here:
// YOu can play animation etc.
}
void OnMouseOver () {
if (distance <= 2.0f)
{
distanceValue = true;
pickTouurchText.text = "PICK TOURCH";
}
}
void OnMouseExit () {
distanceValue = false;
pickTouurchText.text = "";
}
Hope this Helps!
thanks for the answers guys unfortunately while trying to fix this myself i’ve gotten it to a slightly broken point cant pick up the torch at all now, however i think i know how to fix it but i need to access a variable from another script which im not sure how to do (im super new to this)