I have 2 scripts using both the same keys “Fire1” and “E” to perform different actions (shooting and grabbing an item).
// SHOOT SCRIPT
void Update()
{
timer += Time.deltaTime;
if (Input.GetButtonDown("Fire1") && timer >= timeBetweenBullets )
{
ShootPlus();
}
}
// GRAB SCRIPT
void Update()
{
playerDistance = Vector3.Distance(item.transform.position, grabber.transform.position);
if (playerDistance >= grabDistance)
{
isGrabbed = false;
}
if (isGrabbed == true)
{
item.GetComponent<Rigidbody>().velocity = Vector3.zero;
item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
item.transform.SetParent(grabber.transform);
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().detectCollisions = true;
if (Input.GetButton("E"))
{
isGrabbed = false;
}
if (Input.GetButton("Fire1"))
{
transform.Rotate(new Vector3 (0,100,0) * Time.deltaTime);
}
}
void OnMouseOver()
{
if (playerDistance <= grabDistance && structureHealth.DOF >= 3)
{
canGrab = true;
}
if (canGrab == true && Input.GetButtonDown("E"))
{
canGrab = false;
isGrabbed = true;
}
}
Desired behaviour:
- if the item is not grabbed and player use “Fire1”=> player can shoot
- if item is grabbable and “E” is pressed => “isGrabbed = true” and the item is grabbed
- if “isGrabbed = true” => the player cannot shoot. Instead, if “Fire1” is pressed, the grabbed item rotates
- while the item is grabbed, if “E” is pressed the item is released and the player can shoot again
I guess it’s only a matter of logic or using the correct “flags” but I couldn’t be able to solve the problem. Anyone can help please?