Question by
LBardmask · Oct 12, 2021 at 02:53 AM ·
raycastinputif-statements
how to use the new input system inside a raycast if statement
i have a problem with the new input system in unity, i have a button setup on the e key with a passthrough. the defInput.Player.Interact.performed works as expected but the .started and .canceled dont work at all in the if statement. ive racked my brain and i cant figure a way to only call the pickup function if the key isnt being held, but it must be sent from inside the if statement ( cant pick up item if im not looking at it, of course). any help would be apreciated.
heres the problem code:
public class ItemHover : MonoBehaviour
{
public float range = 5f;
public GameObject itemUI;
public Text ItemInfo;
public LayerMask mask;
public float pickUp;
private DefaultInput defInput;
// Start is called before the first frame update
void Awake()
{
defInput = new DefaultInput();
defInput.Player.Interact.started += e => pickUp = e.ReadValue<float>();
defInput.Enable();
}
// Update is called once per frame
void FixedUpdate()
{
RaycastHit hit;
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, range, mask))
{
if (hit.collider.tag == "groundItem")
{
GroundItem gItem = hit.collider.gameObject.GetComponent<GroundItem>();
itemUI.SetActive(true);
ItemInfo.text = gItem.itemName + " (" + gItem.amount + ")";
if(pickUp != 0)
{
gItem.PickUp();
}
}
else
{
itemUI.SetActive(false);
}
}
else
{
itemUI.SetActive(false);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Raycast "If" statements conflicting with each other. 0 Answers
Raycasting not detecting collider 0 Answers
How do you detect if a touch is hitting an object? 0 Answers
Stubborn Pick Up Script 0 Answers